0
我有一個GridView,其中有10列。10我有5列命名爲Ata,Hplan,Hosp,其他(這4個是checkboxfield),其他顏色(Boundfield),再次Alta,Hplan等..現在我想要在用戶下拉選擇要麼5列添加或所有10 ..我需要禁用gridview視圖狀態?我怎麼能實現這一點和第二個問題是我怎麼能動態連續數據綁定event.Basic東西時綁定這些列的是,我希望gridview的列名來複制,但是在數據表中我不能給重複列names.please幫助..從asp.net動態數據表中動態綁定gridview列
protected void Btnshow_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex==1)
{
dtgridbind.Columns.AddRange(new DataColumn[10] { new DataColumn("1_ALta", typeof(bool)),new DataColumn("1_Hplan", typeof(bool)),
new DataColumn("1_Hosp",typeof(bool)),new DataColumn("1_other",typeof(bool)),new DataColumn("1_othercolor",typeof(string)),
new DataColumn("2_ALta", typeof(bool)),new DataColumn("2_Hplan", typeof(bool)),
new DataColumn("2_Hosp",typeof(bool)),new DataColumn("2_other",typeof(bool)),new DataColumn("2_othercolor",typeof(string))});
dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red");
dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red");
dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red");
GridView1.DataSource = dtgridbind;
GridView1.DataBind();
// ViewState["modechange"] = 1;
}
else
{
dtgridbind.Columns.AddRange(new DataColumn[5] { new DataColumn("1_ALta", typeof(bool)),new DataColumn("1_Hplan", typeof(bool)),
new DataColumn("1_Hosp",typeof(bool)),new DataColumn("1_other",typeof(bool)),new DataColumn("1_othercolor",typeof(string))});
dtgridbind.Rows.Add(true, false, false, false, "N/A");
dtgridbind.Rows.Add(true, false, false, false, "N/A");
dtgridbind.Rows.Add(true, false, false, false, "Red");
// ViewState["grid"] = dtgridbind;
GridView1.DataSource = dtgridbind;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (DataColumn datacol in dtgridbind.Columns)
{
if (datacol.ColumnName.Contains("_othercolor"))
{
BoundField bfield = new BoundField();
bfield.HeaderText = datacol.ColumnName.Substring(2, datacol.ColumnName.Length - 2);
bfield.DataField = datacol.ColumnName;
GridView1.Columns.Add(bfield);
}
else
{
CheckBoxField ck = new CheckBoxField();
ck.HeaderText = datacol.ColumnName.Substring(2, datacol.ColumnName.Length - 2);
ck.DataField = datacol.ColumnName;
GridView1.Columns.Add(ck);
}
}
}
heyyy ..thanks buddy..but蔭沒有得到內部headercell.text.I任何文本還沒有gridview.I靜態定義列真不知道是什麼原因這是發生..有人知道? –
我在帖子的底部添加了結果的屏幕截圖。這與你得到的結果相符嗎?數據表綁定到gridview,因此列名將是headercell文本。當我調試代碼時,headercell.text填充了數據表列名稱。 –