简体   繁体   中英

Give the gridview columns a alias name in C#

I have a GridView which is not DataSource bound. At run time this GridView shows some rows at run time. There is a requirement in which user should be able to change the column header text at run time. So i thought of to implement in this way -

User will double click [or single click] on column header and a text box will be visible to user, where user will enter the new text and as soon as user leaves the text box, new column header text will be set as HeaderText property of the column. Can this achieved? can anyone share the sample code to achieve the same? I will be highly obliged to you. Any help will be appreciable.

This is my grid

 <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>

Here is the thing that you can do to bind alias with the column Below is aspx code

<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>

And code to bind data to grid in cs is below

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;
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM