I have a stored procedure that returns a few columns, I am only interested in building a gridview with the ProjectID values that are null. My code below returns all the rows of all the columns, it is just supposed to return where column = projectID. Can someone take a look at it. Thanks
DataRowCollection rowCollection = spDataTable.Rows;
DataTable dt = new DataTable();
foreach (DataColumn col in spDataTable.Columns)
{
if (col.ColumnName == "ProjectID")
{
dt.Columns.Add(col.ColumnName);
lb_Test.Items.Add(col.ColumnName);
foreach(DataRow ros in col.Table.Rows)
{
foreach (object oObj in ros.ItemArray)
{
if (oObj != null)
{
if (col.ColumnName == "ProjectID")
{
lb_Test.Items.Add(oObj.ToString());
dt.Rows.Add(oObj);
}
}
else
{
lb_Test.Items.Add("Null");
}
}
}
grd_test.DataSource = dt;
grd_test.DataBind();
}
else
{
lb_Test.Items.Add("Not valid Name");
}
}
If I am understanding the question, seems like all you need is this…
This will populate the rowCollection with all the rows where the value in the ProjectID column is null.
Having said this, if you can, you may consider changing your stored procedure to only returns the rows you want. This will save network traffic.
Also, a DataTable has built-in filtering capabilities…http://msdn.microsoft.com/en-us/library/zk13kdh0(v=vs.71).aspx. Maybe this would be a better approach.
DataViews also have the capability to filter data. Then you can bind your GridView to a DataView…
https://stackoverflow.com/questions/10893860/how-to-filter-gridview-from-textbox
There are several ways to filter DataTables that are built into .Net. There is no need to roll your own.