Click here to Skip to main content
15,886,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to bind dynamic headers from one data table in gridview and dynamic first column data is from another data table and remaining columns as checkboxes.

can u tell me how to bind this below senario in dynamic.

User | Test1|Test2|Test3|Test4|Test5|
-------------------------------------
Name1 | Checkbox1|Checkbox2|CheckBox3|

Name2 | Checkbox1|Checkbox2|Checkbox3

What I have tried:

source code
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID"
    AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
    OnRowUpdating="GridView1_RowUpdating"
    OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowEditing="GridView1_RowEditing"
    HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">

    <Columns>

        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />
        <asp:BoundField DataField=""  />


        <asp:CommandField ShowEditButton="True" />

        <asp:CommandField ShowDeleteButton="True" />

    </Columns>
</asp:GridView>


code behind code :
private void Bind_CheckBoxList()
{
    DataTable dtusers;
    DataTable dtregions;
    //String SQL = "SELECT P.ID ID, P.Name Name, P.Description Description, B.REGION_ID, B.REGION" +
    //            " FROM Product P, REGION_CONFIG B" +
    //            " WHERE P.BrandID=B.REGION_ID";

    String SQLUsers = "select [USER_ID],USER_FIRST_NAME,USER_LAST_NAME from  USER_INFO";
    String SQLRegions = "select REGION_ID,REGION from REGION_CONFIG";


    string sConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(sConstr))
    {
        using (SqlCommand comm = new SqlCommand(SQLUsers, conn))
        {
            conn.Open();
            using (SqlDataAdapter dausers = new SqlDataAdapter(comm))
            {
                dtusers = new DataTable("tbl");
                dausers.Fill(dtusers);
            }
        }
        using (SqlCommand comm = new SqlCommand(SQLRegions, conn))
        {
            using (SqlDataAdapter daregions = new SqlDataAdapter(comm))
            {
                dtregions = new DataTable("tbl");
                daregions.Fill(dtregions);
            }
        }
    }

    //DataTable dtAll;
    //dtAll = dtusers.Copy();
    //dtAll.Merge(dtregions);

    GridView1.DataSource = dtusers;
    GridView1.DataBind();
}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataTable dt;
    String SQL = "SELECT REGION FROM REGION_CONFIG";

    string sConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(sConstr))
    {
        using (SqlCommand comm = new SqlCommand(SQL, conn))
        {
            conn.Open();
            using (SqlDataAdapter da = new SqlDataAdapter(comm))
            {
                dt = new DataTable("tbl");
                da.Fill(dt);
                //DataRow row;
                //row = dt.NewRow();
                //dt.Rows.Add(row);
               // dt.Columns.Add(new DataColumn("Nametest", typeof(string)));
            }
        }
    }

    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            BoundField boundField = new BoundField();
            if (dt.Columns[i].ColumnName.ToString() != "Name")
            {
                boundField.DataField = dt.Columns[i].ColumnName.ToString();
                boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
                GridView1.Columns.Add(boundField);

            }

        }
    }


    if (e.Row.RowType == DataControlRowType.Header)
    {

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string strRegion;
            strRegion = dt.Rows[i]["REGION"].ToString();
            if (strRegion != "")
            {
                e.Row.Cells[i].Text = dt.Rows[i]["REGION"].ToString();
            }

        }


    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string strRegion;
            strRegion = dt.Rows[i]["REGION"].ToString();
            if (strRegion != "")
            {
                CheckBox cb = new CheckBox();
                e.Row.Cells[i].Controls.Add(cb);
            }

        }


        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //CheckBoxList chkBrandedit = (CheckBoxList)e.Row.FindControl("CheckBoxList1");

            //chkBrandedit.DataSource = dt;
            //chkBrandedit.DataTextField = "Name";
            //chkBrandedit.DataValueField = "ID";
            //chkBrandedit.DataBind();
            //chkBrandedit.SelectedValue = ((DataRowView)e.Row.DataItem)["BrandID"].ToString();
        }
    }
}
Posted
Updated 8-Feb-18 5:27am

1 solution

Hi,

First, you are using an Object Oriented Programming language. You need to make the task easier for you.
Otherwise, your code will be hard to maintain.

Start by creating a new class that represent the data to display in the gridview.
After you fill your objects based on your own logic.
You can either write your own gridview markup, or generate fields dynamicaly :
C#
BoundField newColumnName= new BoundField();

newColumnName.DataField = "yourDataFieldName"; 
newColumnName.Headertext = "yourHeaderName";

yourGridView.Columns.Add(newColumnName);


I dont see any need for a dynamic binding thought.
Dont forget to add
autogeneratedcolumns=false
in your gridview markup
 
Share this answer
 
Comments
AjayReddy32682 8-Feb-18 12:02pm    
can u send me code behind dynamic code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900