我有一个gridview,其中我有10列。在10个我有5列的名称命名为Ata,Hplan,Hosp,其他(这4个是复选框),Othercolor(Boundfield)和Alta,Hplan等等..现在我想在用户下拉选择中添加5列或全部10 ..我需要禁用gridview的viewstate吗?我怎样才能实现这一点......第二个问题是如何在行数据绑定期间动态绑定这些列event.Basic的事情是我希望gridview列名重复,但在datatable中我不能给出重复的列名。请帮助..
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);
}
}
}
答案 0 :(得分:0)
此代码是否符合您的要求?在此代码中,有一个包含两个值的下拉列表。如果选择“10列”,它将回发并将10列添加到数据表中。如果选择“5列”,它将回发并将5列添加到数据表中。
在OnRowDataBound中,它循环遍历标题单元格,并根据“_”字符拆分文本。然后是第二部分。这仅在您遵循列的命名约定时才有效。
您不能拥有具有相同列名的datacolumns。要解决这个问题,您可以使用OnRowDataBound,只需更改单元格文本,如上所述。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="" Value =""></asp:ListItem>
<asp:ListItem Text="10 Columns" Value ="1"></asp:ListItem>
<asp:ListItem Text="5 Columns" Value ="2"></asp:ListItem>
</asp:DropDownList>
<asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
</form>
</body>
</html>
public partial class WebForm3 : System.Web.UI.Page
{
private DataTable dtgridbind = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell headerCell in e.Row.Cells)
{
headerCell.Text = headerCell.Text.Split('_')[1];
}
}
}
protected void DropDownList1_SelectedIndexChanged(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();
}
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");
GridView1.DataSource = dtgridbind;
GridView1.DataBind();
}
}
}