videosrandomok
videosrandomok

Reputation: 21

I can't delete a specific row in datagridview c# .net

I have a program that load a txt file in a datagridview, but the first line of the file is the name of his columns, i want delete this row. See the example file:

No TMNo EnNo Name GMNo Mode In/Out Antipass ProxyWork DateTime

1 1 00000001 admin 1 Hue DutyOn 0 0 2019-12-18 00:24:05

2 1 00000002 18425444 1 Cara DutyOff 0 0 2019-12-17 16:12:43

In the program i separated the data, i try everything , asked:

 `if(dgv[0,i/*(for previous... i ...)*/].value.tostring()=="EnNo")`

    {
    dgv.rows.removeat(i);
    }

and etc etc

some idea? ty

resume: I want to delete the line that has a different format

i use this code:

btnExportarExcel.Enabled = true;
                string text;
                openFileDialog1.Title = "Abrir archivo";
                openFileDialog1.ShowDialog();
                text = openFileDialog1.FileName;

                var rows = new List<Cargar>();
                var sr = new StreamReader(text);


                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    if (!String.IsNullOrEmpty(s.Trim()))
                    {

                        rows.Add(new Cargar(s));

                    }
                }

                sr.Close();

                dataGridView1.DataSource = rows;

Cargar is a class inside:

public class Cargar
        {
            public int USERID { get; set; }
            public string CHECKTIME { get; set; }
            public string CHECKTYPE { get; set; }
            public string VERIFYCODE { get; set; }
            public string SENSORID { get; set; }
            public string Memoinfo { get; set; }
            public string WorkCode { get; set; }
            public string sn { get; set; }
            public string UserExtFmt { get; set; }



            public Cargar(string str)
            {

                var pant = Form.ActiveForm as Form1;

                string[] separator = { "\t" };
                var arr = str.Split(separator, StringSplitOptions.None);
                DateTime Fecha;
                USERID =Convert.ToInt32( arr[2]);
                CHECKTYPE = arr[6];
                Fecha =Convert.ToDateTime( arr[9]);
                if (CHECKTYPE == "DutyOn")
                    CHECKTYPE = "I";
                else
                    CHECKTYPE = "O";

                CHECKTIME = Convert.ToString( Fecha);
                VERIFYCODE = "15";
                WorkCode = "0";
                UserExtFmt = "0";

            }



        }

Upvotes: 0

Views: 48

Answers (1)

Mikael
Mikael

Reputation: 982

Why not just ignore the header row when you're loading the text file?? You could do something like:

            var lines = File.ReadAllLines(file.FullName);
            for (int i = 0; i < lines.Count(); i++)
            {
                if (!lines[i].Contains("EnNo"))
                {
                    //import here
                }
            }

Is your DGV bound? To what exactly, a datatable?

You could also do:

dgv.Rows[0].Visible = false;

edit---Try this:

        btnExportarExcel.Enabled = true;
            string text;
            openFileDialog1.Title = "Abrir archivo";
            openFileDialog1.ShowDialog();
            text = openFileDialog1.FileName;

            var rows = new List<Cargar>();
            var sr = new StreamReader(text);


            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine();
                if (!String.IsNullOrEmpty(s.Trim()) && !s.Contains("EnNo"))
                {

                    rows.Add(new Cargar(s));

                }
            }

            sr.Close();

            dataGridView1.DataSource = rows;

Upvotes: 1

Related Questions