我有一个不受DataSource约束的GridView。在运行时,此GridView在运行时显示一些行。用户应该能够在运行时更改列标题文本。所以我想以这种方式实施 -
用户将双击[或单击]列标题,用户将看到一个文本框,用户将在其中输入新文本,用户离开文本框后,新列标题文本将被设置为列的HeaderText属性。这可以实现吗?任何人都可以共享示例代码来实现相同的目标吗?我将非常感谢你。任何帮助都会很明显。
这是我的网格
<asp:GridView ID="GdvTestData" runat="server"
class="table table-striped table-responsive table-hover"
onrowdatabound="gv_RowDataBound"
PageSize="100" OnSelectedIndexChanged="GdvTestData_SelectedIndexChanged">
<FooterStyle BorderStyle="Solid" />
</asp:GridView>
答案 0 :(得分:0)
您可以执行以下操作来将别名与列绑定 下面是aspx代码
<asp:DropDownList runat="server" ID="ddlQuery">
<asp:ListItem Text="Query1" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Query2" Value="2"></asp:ListItem>
<asp:ListItem Text="Query3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="txtAlias"></asp:TextBox>
<asp:Button runat="server" ID="btnGetData" Text="GetData" OnClick="btnGetData_Click" />
<asp:GridView runat="server" ID="gcData"></asp:GridView>
将数据绑定到cs中的网格的代码在
之下protected void btnGetData_Click(object sender, EventArgs e)
{
GetData();
}
private void GetData()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Connection String";
string Query = string.Empty;
if (ddlQuery.SelectedValue == "1")
Query = "SELECT * FROM Table1";
else if (ddlQuery.SelectedValue == "2")
Query = "SELECT * FROM Table2";
else if (ddlQuery.SelectedValue == "3")
Query = "SELECT * FROM Table3";
try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = Query;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataTable dtFinal = new DataTable();
foreach (DataColumn cln in dt.Columns)
{
dtFinal.Columns.Add(cln.ColumnName + " " + txtAlias.Text, cln.DataType);
}
foreach (DataRow row in dt.Rows)
{
DataRow dr = dtFinal.NewRow();
for (int i = 0; i < dtFinal.Columns.Count; i++)
{
dr[i] = row[i];
}
dtFinal.Rows.Add(dr);
}
gcData.DataSource = dtFinal;
gcData.DataBind();
}
catch (Exception)
{
throw;
}
}