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
140
How to create WebHirachicalDataGrid Checkbox Dynamically in code Behind.
posted

Hi,

i am working on WebHirarchicalDataGrid and i want to Create the CheckBoxes Dynamically as i want to set Each CheckBox a unique ID.i have Columns the set on Server Side , i want the Same on CheckBox Also.i want to Create BoundCheckBox Field code on code behind. My Front and BackEnd Code is :

<ig:WebHierarchicalDataGrid ID="WebHierarchicalDataGrid1" runat="server" AutoGenerateBands="False"
AutoGenerateColumns="False" DataKeyFields="id" DataMember="Module" EnableViewState="true"
ViewStateMode="Enabled" StyleSetName="Office2007Blue"
InitialExpandDepth="3" InitialDataBindDepth="3"
Height="350px" Width="900px" >
<%--<ClientEvents Initialize="InitializHandler" Click="ClickHandler"/>--%>
<%-- <AutoPostBackFlags RowSelectionChanged="false" />--%>
<%--<AutoPostBackFlags CellSelectionChanged="Of" />--%>
<Columns>
<ig:BoundDataField DataFieldName="id" Hidden="true" DataType="System.Int32" Key="id" Width="100px">
<Header Text="Module ID" />
</ig:BoundDataField>
<ig:BoundDataField DataFieldName="Item" DataType="System.String" Key="Item">
<Header Text="Module Description" />
</ig:BoundDataField>
<ig:BoundDataField DataFieldName="Data" Hidden="true" DataType="System.String" Key="Data">
<Header Text="Operations" />
</ig:BoundDataField>
<%--<ig:BoundCheckBoxField DataFieldName="View" DataType="System.Boolean" Key="View">
<Header Text="View" />
</ig:BoundCheckBoxField>
<ig:BoundCheckBoxField DataFieldName="Update" DataType="System.Boolean" Key="Update">
<Header Text="Update" />
</ig:BoundCheckBoxField>
<ig:BoundCheckBoxField DataFieldName="Delete" DataType="System.Boolean" Key="Delete">
<Header Text="Delete" />
</ig:BoundCheckBoxField>
<ig:BoundCheckBoxField DataFieldName="Save" DataType="System.Boolean" Key="Save">
<Header Text="Save" />
</ig:BoundCheckBoxField>--%>
</Columns>

<Bands>
<%--<ig:Band AutoGenerateColumns="False" DataKeyFields="Item" DataMember="TRN_CODE" Key="ChildBand_0">--%>
<ig:Band AutoGenerateColumns="False" DataKeyFields="ChildId" DataMember="TRN_CODE" Key="ChildBand_0">
<Columns>

<%-- <ig:BoundDataField DataFieldName="ChildTableId" Hidden="false" DataType="System.Int32" Key="ChildTableId">
<Header Text="ChildTableId" />
</ig:BoundDataField>--%>
<ig:BoundDataField DataFieldName="ChildId" Hidden="true" DataType="System.Int32" Key="DataCh">
<Header Text="Child Id" />
</ig:BoundDataField>
<ig:BoundDataField DataFieldName="Item" DataType="System.String" Key="ItemCh">
<Header Text="Child Item" />
</ig:BoundDataField>
<ig:BoundDataField DataFieldName="ParentId" Hidden="true" DataType="System.Int32" Key="idCh">
<Header Text="Parent ID" />
</ig:BoundDataField>
<ig:BoundDataField DataFieldName="Operations" Hidden="false" DataType="System.String" Key="Operations">
<Header Text="Operations" />
</ig:BoundDataField>
<ig:BoundCheckBoxField DataFieldName="Trn_Operations" DataType="System.Boolean" Key="Trn_Operations" >
<%--<Header Text="Operations" />--%>
</ig:BoundCheckBoxField>
<%--<ig:BoundCheckBoxField DataFieldName="ChView" DataType="System.Boolean" Key="ChView">
<Header Text="View" />
</ig:BoundCheckBoxField>
<ig:BoundCheckBoxField DataFieldName="ChUpdate" DataType="System.Boolean" Key="ChUpdate">
<Header Text="Update" />
</ig:BoundCheckBoxField>
<ig:BoundCheckBoxField DataFieldName="ChDelete" DataType="System.Boolean" Key="ChDelete">
<Header Text="Delete" />
</ig:BoundCheckBoxField>
<ig:BoundCheckBoxField DataFieldName="ChSave" DataType="System.Boolean" Key="ChSave">
<Header Text="Save" />
</ig:BoundCheckBoxField>--%>

</Columns>

<Behaviors>
<ig:EditingCore BatchUpdating="True">
<Behaviors>
<ig:RowAdding/>
<ig:RowDeleting/>
<ig:CellEditing/>
</Behaviors>
</ig:EditingCore>
<%--<ig:EditingCore AutoCRUD="false">
<Behaviors>
<ig:CellEditing>
<ColumnSettings>
<ig:EditingColumnSetting ColumnKey="ItemCh" ReadOnly="true" />
</ColumnSettings>

</ig:CellEditing>
</Behaviors>
</ig:EditingCore>--%>
</Behaviors>
</ig:Band>
</Bands>


<Behaviors>

<ig:Selection CellClickAction="Row" CellSelectType="None" RowSelectType="Single">
<AutoPostBackFlags CellSelectionChanged="false" RowSelectionChanged="false" ColumnSelectionChanged="false" />
<%--<AutoPostBackFlags RowSelectionChanged="false" />--%>

</ig:Selection>
<ig:Activation>
</ig:Activation>
<ig:EditingCore BatchUpdating="True">
</ig:EditingCore>
<%--<ig:EditingCore>
<Behaviors>

<ig:CellEditing>
<ColumnSettings>

<ig:EditingColumnSetting ColumnKey="Item" ReadOnly="true" />
</ColumnSettings>

</ig:CellEditing>
</Behaviors>
</ig:EditingCore>--%>
</Behaviors>
</ig:WebHierarchicalDataGrid>

and  code Behind Code is;

private DataTable CreateGridDataSourceForSubModules(string tableName, bool duplicate)
{


DataTable SubModules = Trn_Oprations.GetAllTrnsList();
// Create a new DataTable.
System.Data.DataTable table = new DataTable(tableName);
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;

// Create new DataColumn, set DataType,
// Create First column.
//column = new DataColumn();
//column.DataType = System.Type.GetType("System.Int32");
////column.ColumnName = "Data";
//column.ColumnName = "ChildTableId";
//column.AutoIncrement = false;
//column.Caption = "ChildTableId";
//column.ReadOnly = false;
//column.Unique = false;
//// Add the column to the table.
//table.Columns.Add(column);


column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
//column.ColumnName = "Data";
column.ColumnName = "ChildId";
column.AutoIncrement = false;
column.Caption = "ChildId";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);


// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Item";
column.AutoIncrement = false;
column.Caption = "Item";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);


// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ParentId";
column.ReadOnly = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);


column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Operations";
//column.DefaultValue = false;
table.Columns.Add(column);


column = new DataColumn();
column.DataType = System.Type.GetType("System.Boolean");
column.ColumnName = "Trn_Operations";
table.Columns.Add(column);

//TemplateDataField templateCol = new TemplateDataField();
//templateCol.ItemTemplate = new CustomItemTemplate();
//WebHierarchicalDataGrid1.Columns.Add(templateCol);
//column = new DataColumn();
//column.DataType = System.Type.GetType("System.Boolean");
//column.ColumnName = "ChView";
//column.AutoIncrement = false;
//column.Caption = "View";
////column.ReadOnly = false;
//column.Unique = false;
//column.DefaultValue = false;
//// Add the column to the table.
//table.Columns.Add(column);

//column = new DataColumn();
//column.DataType = System.Type.GetType("System.Boolean");
//column.ColumnName = "ChUpdate";
//column.AutoIncrement = false;
////column.Caption = "Update";
//column.ReadOnly = false;
//column.Unique = false;
//column.DefaultValue = false;
//// Add the column to the table.
//table.Columns.Add(column);


//column = new DataColumn();
//column.DataType = System.Type.GetType("System.Boolean");
//column.ColumnName = "ChDelete";
//column.AutoIncrement = false;
//column.Caption = "Delete";
//// column.ReadOnly = false;
//column.Unique = false;
//column.DefaultValue = false;
//// Add the column to the table.
//table.Columns.Add(column);


//column = new DataColumn();
//column.DataType = System.Type.GetType("System.Boolean");
//column.ColumnName = "ChSave";
//column.AutoIncrement = false;
//column.Caption = "Save";
//column.ReadOnly = false;
//column.Unique = false;
//column.DefaultValue = false;
//// Add the column to the table.
//table.Columns.Add(column);

//for (int j = 0; j < SubModules.Rows.Count; j++)
//{
// string Trn_id = SubModules.Rows[j]["Trn_Id"].ToString();
// DataTable GetTrnOperationsByTrnId = Trn_Oprations.GetOperationsByTrn(Trn_id);

//for (int u = 0; u < GetTrnOperationsByTrnId.Rows.Count; u++)
//{
// BoundCheckBoxField field = new BoundCheckBoxField(true);
// column = new DataColumn();
// field.Key = "Discontinued" + u + 1;
// field.Header.Text = "Discontinued" + u;
// field.DataFieldName = "Discontinued" + u;
// this.WebHierarchicalDataGrid1.
// this.WebHierarchicalDataGrid1.Columns.Add(field);
// // column.Columns.Add(field);
//}
// }

// Make the ID column the primary key column.
if (!duplicate)
{
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;
}
else
{

//DataColumn[] PrimaryKeyColumns = new DataColumn[1];
//PrimaryKeyColumns[0] = table.Columns["ChildTableId"];
//table.PrimaryKey = PrimaryKeyColumns;
}


for (int k = 0; k < SubModules.Rows.Count; k++)
{
string TrnId = SubModules.Rows[k]["Trn_Id"].ToString();
DataTable TrnOperations=Trn_Oprations.GetOperationsByTrn(TrnId);

for (int j = 0; j < TrnOperations.Rows.Count; j++) // it Should Create Each by the count of Trn Operations
{
row = table.NewRow();
// CheckBox cb = new CheckBox();
string a = SubModules.Rows[k]["MODULE_CODE"].ToString();
// row["ChildTableId"] = k;
row["ParentId"] = Convert.ToInt32(a);
row["Item"] = SubModules.Rows[k]["Menu_Label"].ToString();
row["ChildId"] = Convert.ToInt32(SubModules.Rows[k]["Trn_Id"].ToString());
// BoundCheckBoxField ckField = new BoundCheckBoxField();
// ckField.Key = TrnOperations.Rows[j]["Opr_Description"].ToString();
row["Operations"] = TrnOperations.Rows[j]["Opr_Description"].ToString();
//((BoundCheckBoxField)this.WebHierarchicalDataGrid1.Columns["Operations"]).ValueConverter = ;
string OperationName= TrnOperations.Rows[j]["Opr_Description"].ToString();
table.Rows.Add(row);

}
}

return table;

}

  • 1300
    Offline posted

    Hello Muhammad, 

    After investigating this further, I determined that a BoundCheckboxField for the “Save” column of the dataSource could be defined in a method bound to the “OnInit“ event as follows:

     BoundCheckBoxField saveColumn = new BoundCheckBoxField();

            saveColumn.Key = "Save";

            saveColumn.Header.Text = "Save";

            saveColumn.DataFieldName = "Save";

            saveColumn.Hidden = false;

            WebHierarchicalDataGrid1.GridView.Columns.Add(saveColumn);

    Additionally a column could be added to one of the bands the following way:

     BoundCheckBoxField saveColumn = new BoundCheckBoxField();

            saveColumn.Key = "ChSave";

            saveColumn.Header.Text = "Save";

            saveColumn.DataFieldName = "ChSave";

            WebHierarchicalDataGrid1.Bands["ChildBand_0"].Columns.Add(saveColumn);

    A checkbox could be added by defining a TemplateDataField in the method bound to the “OnInit” and adding the checkbox as ItemTemplate: 

      private class CheckboxTemplate : ITemplate

        {

            #region ITemplate Members

            public void InstantiateIn(Control container)

            {

                CheckBox checkbox = new CheckBox();

                checkbox.ID = "checkbox";

                container.Controls.Add(checkbox);

            }

            #endregion

        }

    protected void WebHierarchicalDataGrid1_Init(object sender, EventArgs e)

        {

         TemplateDataField checkboxColumn = new TemplateDataField();

            checkboxColumn.Key = "CheckBox";

            checkboxColumn.ItemTemplate = new CheckboxTemplate();

            WebHierarchicalDataGrid1.GridView.Columns.Add(checkboxColumn);

        }

    Furthermore, the id of the checkbox could be set in a method bound to the “OnInitializeRow” event:

    protected static int id = 0;

        protected void WebHierarchicalDataGrid1_InitializeRow(object sender, RowEventArgs e)

        {

            if (e.Row.Items.FindItemByKey("CheckBox") != null && e.Row.Items.FindItemByKey("CheckBox").FindControl("checkbox") != null)

            {

                e.Row.Items.FindItemByKey("CheckBox").FindControl("checkbox").ID += id;

                id += 1;

            }

        }   

    Below I am attaching a sample, demonstrating the described behavior. Please test it on your side and let me know if you need any further information regarding this matter.

    Regards,

    Monika Kirkova,

    Infragistics

    WebHierarchicalDataGridAddColumnsServerSide.zip