How do I fetch a set of rows from data table
I have a dataset that has two datatables.
In the first datatable I have EmpNo,EmpName and EmpAddress
In the second datatable I have Empno,EmpJoindate, EmpSalary.
I want a result where I should show EmpName as the label and his/her details in the gridview
I populate a datalist with the first table, and have EmpNo as the datakeys.
Then I populate the gridview inside the datatable which has EmpNo,EmpJoinDate and EmpAddress.
My code is some what as below
Datalist1.DataSource = dt;
Datalist1.DataBind();
for (int i = 0; i < Datalist1.Items.Count; i++)
{
int EmpNo = Convert.ToInt32(Datalist1.DataKeys[i]);
GridView gv = (GridView)Datalist1.FindControl("gv");
gv.DataSource = dt2;
gv.DataBind();
}
Now I have a problem, I have to bind the Details of the corresponding Employee to the gridview. Whereas the above code will display all the details of all employees in the gridview.
If we use IEnumerable we give a condition where(a=>a.eno=EmpNo), and bind that list to the gridview.
How do I do this in datatable.
kindly do not give me suggestions of altering the stored procedure which results the values in two tables, because that cannot be altered. I have to find a solution within the existing objects I have.
Regards Hema
Hi, I modified the program as below Igor, instead of datarelation,(my TL did not approve of it) I used IEnumerable
DataTable dt = new DataTable(); dt.Columns.Add("EmpNo"); for (int i = 65; i < 70; i++) { DataRow dr = dt.NewRow(); dr["EmpNo"] = i.ToString(); dt.Rows.Add(dr); }
DataTable dt2 = new DataTable();
dt2.Columns.Add("EmpNo");
dt2.Columns.Add("EmpName");
for (int i = 65; i < 70; i++)
{
DataRow dr = dt2.NewRow();
dr["EmpNo"] = i.ToString();
dr["EmpName"] = Convert.ToChar(i);
dt2.Rows.Add(dr);
}
Datalist1.DataSource = dt;
Datalist1.DataBind();
IEnumerable<DataRow> sequence = dt2.AsEnumerable();
for (int i = 0; i < Datalist1.Items.Count; i++)
{
string EmpNo = Datalist1.DataKeys[i].ToString();
string strExpr = "EmpNo =" + EmpNo.ToString();
GridView Gridview1 = (GridView)Datalist1.Items[i].FindControl("Gridview1");
Gridview1.DataSource = sequence.Where(t => t.Field<string>("EmpNo") == EmpNo);
Gridview1.DataB开发者_如何学Goind();
}
My Designer.aspx is as below
<asp:DataList ID="Datalist1" runat="server" DataKeyField="EmpNo">
<ItemTemplate>
<asp:GridView ID="Gridview1" runat="server">
</asp:GridView>
</ItemTemplate>
</asp:DataList>
On execution I do not observe any results, and I see errors as
RowError HasErrors on the screen
I included in the gridview the bound field control as
<asp:GridView ID="Gridview1" AutoGenerateColumns="true" runat="server">
<Columns>
<asp:BoundField DataField="EmpNo" />
</Columns>
</asp:GridView>
And on execution, it throws an error stating A field or property with the name 'EmpNo' was not found on the selected data source.
I checked the "sequence" using Debug mode, and it had values in the array, which I have highlighted,
So where am I going wrong
- Results View Expanding the Results View will enumerate the IEnumerable
- [0] {System.Data.DataRow} System.Data.DataRow HasErrors false bool
- ItemArray {object[2]} object[] [0] "65" object {string} [1] "A" object {string} RowError "" string RowState Added System.Data.DataRowState
- Table {} System.Data.DataTable
- Static members
- Non-Public members
1) you have to add relation between these two tables: Master and Detail (use DataSet editor).
2) then use relation to retrieve related records, for example:
DataRelation customerOrdersRelation =
customerOrders.Relations.Add("CustOrders",
customerOrders.Tables["Customers"].Columns["CustomerID"],
customerOrders.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow custRow in customerOrders.Tables["Customers"].Rows)
{
Console.WriteLine(custRow["CustomerID"].ToString());
foreach (DataRow orderRow in custRow.GetChildRows(customerOrdersRelation))
{
Console.WriteLine(orderRow["OrderID"].ToString());
}
}
In your case you should assign the DataSource of detailt datagrid to currentMasterRow.GetChildRows(masterDetailRelation)
精彩评论