Как связать динамический заголовок и столбцы в gridview
как связать динамические заголовки из одной таблицы данных в gridview и динамические данные первого столбца из другой таблицы данных и оставшихся столбцов в качестве флажков.
можете ли вы сказать мне, как связать это ниже senario в динамике.
Пользователь | Test1|Test2|Test3|Test4|Test5|
-------------------------------------
Name1 | Checkbox1|Checkbox2|CheckBox3|
Name2 | Checkbox1|Checkbox2|Checkbox3
Что я уже пробовал:
исходный код
<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>
код за кодом :
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(); } } }