简体   繁体   中英

Convert DataTable Row To DataGridView Row

I have a DataTable filled out from a database and I need to add to the data GridView specific Rows from the DataTable according to a condition (if statement).

I have the following code but it does not work and I could not make casting from DataTable Row To DataGridViewRow.

for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (Convert.ToDateTime(dt.Rows[i][5].ToString()) < Convert.ToDateTime("00:05:00"))
                    {  
                        dataGridView1.Rows.Add(dt.Rows[i]);
                    }
                }

DataGridView.Rows.Add method accepts array of objects as a parameter.

So you could add rows to DatagridView using ItemArray property of a DataRow .

dataGridView1.Rows.Add(dt.Rows[i].ItemArray);

You can use :

DataRow[] rows = dt.Select("NameOfColumn < '00:05:00'");
dataGridView1.DataSource = rows.CopyToDataTable();

With " NameOfColumn " represent for you " dt.Rows[i][5] ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM