user1998735 user1998735 - 29 days ago 8
C# Question

adding datarow[] array to a datagridview using c# forms

I currently have the ff codes. My problem is how do I get the datarow[] array data and show it to a datagridview table.

DataSet ds = new DataSet();
Data dt = ds.Tables["Tables"];
string path = Application.StartupPath + "\\test.xml";
Int MdNum = 1;

//assign xmlfile to data set
ds.ReadXml(path);

//create datarow array and get tables that has md_id=1
DataRow[] result = dt.Select("[MD_ID] = " + MdNum);

//create datagrid columns
dataGridView2.Columns.Add("Column", "TBL_ID");
dataGridView2.Columns.Add("Column", "TBL_Name");
dataGridView2.Columns.Add("Column", "MD_ID");
dataGridView2.Columns.Add("Column", "IsRequired");

Answer

Create view and set it as datasource

DataView view = new DataView(dt);

view.RowFilter = "MD_ID = " +MdNum;

dataGridView2.DataSource = view;

Or you can use CopyToDataTable method

dataGridView2.DataSource = result.CopyToDataTable();