Answered by:
Linq to datatable problem

Question
-
User1074540480 posted
Private Sub grdvContractList_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdvContractList.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim gvTemp As GridView = CType(Me.FindControl("ctl00$ContentPlaceHolder1$grdvContractList$ctl02$grdvContractInfo"), GridView) Dim strContractID As String = CType(Me.FindControl("ctl00$ContentPlaceHolder1$grdvContractList$ctl02$hdnContractID"), HiddenField).Value Dim ds = From lst In dtContractList.AsEnumerable() _ Where lst.Field(Of Integer)("ContractID") = Integer.Parse(strContractID) _ Select lst
gvTemp.DataSource = ds.ToList gvTemp.DataBind() End If End SubAbove is my code.
I just want to get data records with specific contract ID.
gvTemp is nested gridview in grdvContractList. I have another hiddenfield to save contractID.
When grdvContractList_RowDataBound, datasource of gvTemp is automatically assigned.
My problem is I gets all the records from dtContractList instead of specific contractID.
Please take a look at my code. Thank you.
Wednesday, January 22, 2014 3:23 PM
Answers
-
User-1634784673 posted
DataTable table = new DataTable(); table.Columns.Add("No",typeof(int)); table.Columns.Add("Sqr", typeof(int)); for (int i = 1; i < 11; i++) { DataRow row = table.NewRow(); row[0] = i; row[1] = (i * i); table.Rows.Add(row); } var Result = (from t in table.AsEnumerable() where t.Field<int>("No") == 2 select t).CopyToDataTable(); GridViewFilter.DataSource = Result; GridViewFilter.DataBind();
Try the above code snippet, it returns only one row in the Result variable used to bind to grid view.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 23, 2014 4:20 AM
All replies
-
User-1634784673 posted
DataTable table = new DataTable(); table.Columns.Add("No",typeof(int)); table.Columns.Add("Sqr", typeof(int)); for (int i = 1; i < 11; i++) { DataRow row = table.NewRow(); row[0] = i; row[1] = (i * i); table.Rows.Add(row); } var Result = (from t in table.AsEnumerable() where t.Field<int>("No") == 2 select t).CopyToDataTable(); GridViewFilter.DataSource = Result; GridViewFilter.DataBind();
Try the above code snippet, it returns only one row in the Result variable used to bind to grid view.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 23, 2014 4:20 AM -
User1074540480 posted
Thank you so much, maruthishang.... You are correct.
Adding ".CopyToDataTable();" works!
I have one more question. I want to choose multiple columns from dtContractList and assigned it as datasource to a gridview.
My code is like below:
Dim dtContractList_1 = (From o In dtContractList.AsEnumerable _ Select New With { _ .ContractNumber = o.Item("ContractNumber"), .ContractName = o.Item("ContractName"), .OrganizationName = o.Item("OrganizationName"), .ContractID = o.Item("ContractID")}).Distinct
But error occured.
Thursday, January 23, 2014 11:16 AM