iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
Hi
I have a Dataset with 2 DataTables (Table A and Table B).
On the Page they are 2 WebGrids (WebGrid A and WebGrid B)
Now, I want bind WebGrid A to Table A and WebGrid B to Table B.
How can I do this?
I have written the following Code:
On Page_Load I fill the Dataset with 2 Tables
protected void Page_Load(object sender, EventArgs e) { ds = GetData(); }
GetData() has 2 Select-Statement:
SELECT ID AS IDTableA FROM TableA
SELECT ID AS IDTableB FROM TableB
On InitializeDatasSource (similar for Table B):
protected void WebGridA_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { e.DataSource = ds.Tables["TableA"]; }
On PrepareDataBinding (similar for Table B):
protected void WebGridA_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { if (!Page.IsPostBack) { WebGridA.RetrieveStructure(); } }
If I run the project, I get an exception "Can't find DataColumn with name 'IDTableA' in the specified datasource"
I have also tried to set DataMember = TableA/TableB in PrepareDataBinding-Event. Then both WebGrid have the same Data!
How can I bind 2 WebGrids to Dataset?
Thanks for help.
Michael
Hi James
Sorry, I was confused about DataMember="DataSetA".
At the second reading I've noticed that you mean the DataTable...
Here is the complete and functional code:
private DataSet ds; protected void Page_Load(object sender, EventArgs e) { ds = GetData(); } protected void grd1_PrepareDataBinding(object sender, DataSourceEventArgs e) { if (!Page.IsPostBack) { grd1.ClearCachedDataSource(); grd1.RetrieveStructure(); } } protected void grd1_InitializeDataSource(object sender, DataSourceEventArgs e) { grd1.DataMember = "<DataTable1>"; grd1.RootTable.DataMember = "<DataTable1>"; e.DataSource = ds; } protected void grd2_PrepareDataBinding(object sender, DataSourceEventArgs e) { if (!Page.IsPostBack) { grd2.ClearCachedDataSource(); grd2.RetrieveStructure(); } } protected void grd2_InitializeDataSource(object sender, DataSourceEventArgs e) { grd2.DataMember = "<DataTable2>"; grd2.RootTable.DataMember = "<DataTable2>"; e.DataSource = ds; }
Thank you, James!
Regards
Looks like you missed out some of the code. Could you paste the complete code.
From your error description it looks like, may be you have set the same DataKeyField IDTableA for both the grids and this might be creating a problem as the other grid does not have this column in its datasource.
Here is the original code.
Let me explain:
I have a table with Productionlines and a table with Computers. Enduser can define, which Computer works on which Productionline (Multiple choice is possible). Userinputs are stored in third table called by ComputerLine. On this way, I have two 1:n relationships (see DB-Diagram in attached file).
On Page I want display Productionlines and Computers in an own table. Enduser should be able to link the records in these tables. I would prefer to Drag and Drop, but I have also trouble (see DragDrop (WebGrid to WebTreeview) using WebPaneManager)
The easiest way to solve this issue: I load tables in separate Datasets, but I'm not happy with this idea...
How would you implement this scenario?
private DataSet ds; protected void Page_Load(object sender, EventArgs e) { ds = GetData(); } protected void grdLine_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { e.DataSource = ds.Tables["Line"]; } protected void grdLine_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { if (!Page.IsPostBack) { grdLine.RetrieveStructure(); } } protected void grdPC_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { e.DataSource = ds.Tables["Computer"]; } protected void grdPC_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { if (!Page.IsPostBack) { grdPC.RetrieveStructure(); } } protected void grdComputerLine_InitializeDataSource(object sender, DataSourceEventArgs e) { e.DataSource = ds.Tables["ComputerLine"]; } protected void grdComputerLine_PrepareDataBinding(object sender, DataSourceEventArgs e) { if (!Page.IsPostBack) { grdComputerLine.RetrieveStructure(); WebGridColumn colID = grdComputerLine.RootTable.Columns[0]; colID.Width = Unit.Pixel(100); colID.ColumnType = ColumnType.Text; WebGridColumn colLineID = grdComputerLine.RootTable.Columns[1]; colLineID.Width = Unit.Pixel(100); colLineID.ColumnType = ColumnType.Text; } } public DataSet GetData() { // Connection SqlConnection cn = new SqlConnection(); cn.ConnectionString = @"xxx"; cn.Open(); using (cn) { SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "SELECT ID,Name FROM Production.Line; " + "SELECT ID,Name FROM Production.Computer; " + "SELECT ComputerID,LineID FROM Maintenance.ComputerLine"; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.TableMappings.Add("Table", "Line"); da.TableMappings.Add("Table1", "Computer"); da.TableMappings.Add("Table2", "ComputerLine"); DataSet ds = new DataSet(); da.Fill(ds); da.FillSchema(ds, SchemaType.Source); DataRelation rel; rel = new DataRelation("relLineComputerLine", ds.Tables["Line"].Columns["ID"], ds.Tables["ComputerLine"].Columns["LineID"]); ds.Relations.Add(rel); rel = new DataRelation("relComputerComputerLine", ds.Tables["Computer"].Columns["ID"], ds.Tables["ComputerLine"].Columns["ComputerID"]); ds.Relations.Add(rel); return ds; }
Michael,
Yes, you can easily bind two WebGrids into a dataset. Make sure the Grid's DataMember and the RootTable's DataMember is assigned to the target datatable's name in the Grid definition (aspx). Then, your InitializeDataSource should be pointed to a dataset, not to a datatable.
Example:
<ISWebGrid:WebGrid ID="grdPC" runat="server" DataMember="DataSetA" ...> <RootTable DataMember="DataSetA" ...> ... </RootTable> </ISWebGrid:WebGrid>
Then for InitializeDataSource:
protected void grdPC_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { e.DataSource = ds; }
This way, WebGrid will be able to dispatch the target data table from the dataset, based on the given data member that bound to the WebGrid's table.
Let me know how it goes in your end.
James.
I can't follow your explanation.
In your Example, you bind always to Dataset, but where have I choose the Data Table?
I have also problems with following Scenario:
How can I change DataTable, if I have a Dataset with 3 Tables? The WebGrid shows always Data from the first added Table (to Dataset).
Thanks
WebGrid will use the first data table in the DataSet only when the DataMember is not specified.
So if you have 3 data tables in your DataSet, you can have WebGrid to display one of the data tables by specifying the DataMember in the WebGrid and RootTable object, which is shown in my previous sample code.
For your convenience, let me paste the code where you can specify the data table to be displayed.
Hope this helps,James.
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname