Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
425
Dynamical data binding
posted

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.DataKeyFields = WebHierarchicalDataGrid1.Columns[0].Key;

WebHierarchicalDataGrid1.DataSource = (B[])data;

WebHierarchicalDataGrid1.DataBind();

}

}

 

 

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.DataBind();

 

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)

{

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 A";

dtf1.Key = "valueA";

dtf1.DataFieldName = "valueA";

 

WebHierarchicalDataGrid1.Columns.Add(dtf);

WebHierarchicalDataGrid1.Columns.Add(dtf1);

 

A[] valueList = new A[10];

 

for (int i = 0; i < 10; i++)

{

A obj = new A();

obj.uniqueID = i;

obj.valueA = "Value A " + i.ToString();

 

valueList[i] = obj;

}

 

 

WebHierarchicalDataGrid1.DataKeyFields = WebHierarchicalDataGrid1.Columns[0].Key                WebHierarchicalDataGrid1.DataSource = valueList;

WebHierarchicalDataGrid1.DataBind();

 

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;

}

}

 

 

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

{

Int64 _uniqueID;

public Int64 uniqueID

{

get { return _uniqueID; }

set { _uniqueID = value; }

}

 

string _valueB;

public string valueB

{

get { return _valueB; }

set { _valueB = value; }

}

}

Thanks in advanced.

Parents
No Data
Reply
  • 17590
    Offline posted

    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.

    WHDG.zip
Children