Hello togheter,
could somebody please support me by the following data binding issue:
On my web site I have two buttons (A & B).
When I click button A, I would like to bind data source A with the dedicated columns to the WebHierarchicalDataGrid.
When I click button B, I would like to bind data source B with the dedicated columns to the WebHierarchicalDataGrid.
HTML:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<ig:WebHierarchicalDataGrid ID="WebHierarchicalDataGrid1" runat="server" Height="500px" Width="400px">
<GroupingSettings EnableColumnGrouping="True" GroupAreaVisibility="Visible" GroupByAreaLocation="Top"/>
<Behaviors>
<ig:Activation Enabled="true"></ig:Activation>
<ig:Selection Enabled="true" RowSelectType="Single" CellClickAction="Row" ></ig:Selection>
<ig:RowSelectors Enabled="true" RowNumbering="true" EnableInheritance="true"></ig:RowSelectors>
<ig:Paging Enabled="true" EnableInheritance="true" PagerMode="Numeric" PageSize="100"></ig:Paging>
<ig:Sorting Enabled="true" EnableInheritance="true" SortingMode="Single"></ig:Sorting>
</Behaviors>
</ig:WebHierarchicalDataGrid>
<asp:Button ID="Button_A" runat="server" Text="Button_A" OnClick="Button_A_Click" />
<asp:Button ID="Button_B" runat="server" Text="Button_B" OnClick="Button_B_Click" />
</div>
</form>
</body>
Page_Load:
if(!Page.IsPostBack)
{
Session["DataSource_Session"] = null;
WebHierarchicalDataGrid1.AutoGenerateColumns = false;
}
object data = Session["DataSource_Session"];
if(data != null)
if (data.GetType() == typeof(A[]))
WebHierarchicalDataGrid1.DataKeyFields = WebHierarchicalDataGrid1.Columns[0].Key;
WebHierarchicalDataGrid1.DataSource = (A[])data;
WebHierarchicalDataGrid1.DataBind();
else if (data.GetType() == typeof(B[]))
WebHierarchicalDataGrid1.DataSource = (B[])data;
Button_Click_Event:
protected void Button_B_Click(object sender, EventArgs e)
try
WebHierarchicalDataGrid1.Columns.Clear();
WebHierarchicalDataGrid1.GridView.ClearDataSource();
BoundDataField dtf = new BoundDataField();
dtf.Header.Text = "Unique ID";
dtf.Key = "uniqueID";
dtf.DataFieldName = "uniqueID";
BoundDataField dtf1 = new BoundDataField();
dtf1.Header.Text = "Value B";
dtf1.Key = "valueB";
dtf1.DataFieldName = "valueB";
WebHierarchicalDataGrid1.Columns.Add(dtf);
WebHierarchicalDataGrid1.Columns.Add(dtf1);
B[] valueList = new B[10];
for (int i = 0; i < 10; i++)
B obj = new B();
obj.uniqueID = i;
obj.valueB = "Value B " + i.ToString();
valueList[i] = obj;
WebHierarchicalDataGrid1.DataKeyFields = WebHierarchicalDataGrid1.Columns[0].Key WebHierarchicalDataGrid1.DataSource = valueList;
WebHierarchicalDataGrid1.GridView.DataKeyFields = WebHierarchicalDataGrid1.DataKeyFields;
WebHierarchicalDataGrid1.GridView.DataSource = WebHierarchicalDataGrid1.DataSource;
WebHierarchicalDataGrid1.GridView.DataBind();
WebHierarchicalDataGrid1.RefreshBehaviors();
Session["DataSource_Session"] = valueList;
catch (Exception ex)
string msg = ex.Message;
protected void Button_A_Click(object sender, EventArgs e)
dtf1.Header.Text = "Value A";
dtf1.Key = "valueA";
dtf1.DataFieldName = "valueA";
A[] valueList = new A[10];
A obj = new A();
obj.valueA = "Value A " + i.ToString();
Data_Source_Class:
public class A
Int64 _uniqueID;
public Int64 uniqueID
get { return _uniqueID; }
set { _uniqueID = value; }
string _valueA;
public string valueA
get { return _valueA; }
set { _valueA = value; }
public class B
string _valueB;
public string valueB
get { return _valueB; }
set { _valueB = value; }
Thanks in advanced.
Hello help123,
Thank you for posing in our community.
I made a small sample from the code snippets provided. I noticed that you are trying to set the data source for the WebHierarchialDataGrid multiple times. I removed the additional data bindings and on my side the sample was working as expected.
I am attaching the sample project for your reference. Please test this on your side and let me know whether it helps you achieve your requirement.
Please feel free to contact me if you have any additional questions regarding this matter.
Hello,
thank you for the fast support. Unfortunatelly it is not working properly on my site. The pageing, sorting and grouping doesn't work as expected.
I have a working solution with 'AutoGenerateColumns = true' (see below). All I need is to substitute the 'AutoGenerateColumns = true' with BoundDataField:
e.G.:
BoundDataField dtf = new BoundDataField(); dtf.Header.Text = "Unique ID"; dtf.Key = "uniqueID"; dtf.DataFieldName = "uniqueID"; BoundDataField dtf1 = new BoundDataField(); dtf1.Header.Text = "Value A"; dtf1.Key = "valueA"; dtf1.DataFieldName = "valueA";
BoundDataField dtf = new BoundDataField();dtf.Header.Text = "Unique ID";dtf.Key = "uniqueID";dtf.DataFieldName = "uniqueID";BoundDataField dtf1 = new BoundDataField();dtf1.Header.Text = "Value B";dtf1.Key = "valueB";dtf1.DataFieldName = "valueB";
Could you please show me how to solve the problem? P.S.: I am using Infragsitics V 14.1.
Here the working code.
if (!IsPostBack) { Session["DataSource_Session"] = null; WebHierarchicalDataGrid1.AutoGenerateColumns = true; } object data = Session["DataSource_Session"]; if (data != null) { if (data.GetType() == typeof(A[])) { WebHierarchicalDataGrid1.DataKeyFields = "uniqueID"; WebHierarchicalDataGrid1.DataSource = (A[])data; WebHierarchicalDataGrid1.DataBind(); } else if (data.GetType() == typeof(B[])) { WebHierarchicalDataGrid1.DataKeyFields = "uniqueID"; WebHierarchicalDataGrid1.DataSource = (B[])data; WebHierarchicalDataGrid1.DataBind(); } }
Button_Event:
protected void Button_A_Click(object sender, EventArgs e) { WebHierarchicalDataGrid1.GridView.ClearDataSource(); A[] valueList = new A[1000]; for (int i = 0; i < 1000; i++) { A obj = new A(); obj.uniqueID = i; obj.valueA = "Value A " + i.ToString(); valueList[i] = obj; } WebHierarchicalDataGrid1.DataKeyFields = "uniqueID"; WebHierarchicalDataGrid1.DataSource = valueList; WebHierarchicalDataGrid1.DataBind(); Session["DataSource_Session"] = valueList; }
protected void Button_B_Click(object sender, EventArgs e) { WebHierarchicalDataGrid1.GridView.ClearDataSource(); B[] valueList = new B[1000]; for (int i = 0; i < 1000; i++) { B obj = new B(); obj.uniqueID = i; obj.valueB = "Value B " + i.ToString(); valueList[i] = obj; } WebHierarchicalDataGrid1.DataKeyFields = "uniqueID"; WebHierarchicalDataGrid1.DataSource = valueList; WebHierarchicalDataGrid1.DataBind(); Session["DataSource_Session"] = valueList; }
Model and HTML:
No changes!
Thanks in advanced!