Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear All,

I have done many projects using Gridview as it is one of the most commonly used controls in ASP.Net. But, now I have different requirement. i.e., Making Gridview column values as Link button
when AutoGeneratedColumns is true.

Below is the aspx for the same:

XML
<asp:GridView ID="GridView1"
                AutoGenerateColumns="true"
                runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowBound">
            </asp:GridView>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GridTestSqlConnectionString %>" SelectCommand="SELECT * FROM [tblEmployee]"></asp:SqlDataSource>



Any help with a code snippet would be greatly appreciated.

Regards,
Posted

 
Share this answer
 
v2
 
Share this answer
 
The given below is the example.Go through this.
In .ASPX page:

C#
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
          <columns>
              <asp:templatefield headertext="L1">
                  <itemtemplate><asp:label id="lbl1" runat="server" text="<%# Bind("L1") %>"></asp:label></itemtemplate>
              </asp:templatefield>
               <asp:templatefield headertext="L1">
                  <itemtemplate><asp:label id="lbl2" runat="server" text="<%# Bind("L2") %>"></asp:label></itemtemplate>
              </asp:templatefield>
               <asp:templatefield headertext="L1">
                  <itemtemplate><asp:linkbutton id="lbl3" runat="server" text="<%# Bind("L3") %>"></asp:linkbutton></itemtemplate>
              </asp:templatefield>
          </columns>
      </asp:gridview>


In .CS page:

C#
protected void Page_Load(object sender, EventArgs e)
    {

        DataTable table1 = new DataTable("DTBLE1");
        table1.Columns.Add("L1");
        table1.Columns.Add("L2");
        table1.Columns.Add("L3");
        table1.Rows.Add("1", 5);
        table1.Rows.Add("3", 5);
        table1.Rows.Add("4", 9);
        table1.Rows.Add("7", 3);
        table1.Rows.Add("6", 6);

        DataSet Dset = new DataSet("DSET1");
        Dset.Tables.Add(table1);

        if (Dset.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < Dset.Tables[0].Rows.Count; i++)
            {
                DataRow dr = Dset.Tables[0].Rows[i];
                int a = Convert.ToInt32(dr["L1"]);
                int b = Convert.ToInt32(dr["L2"]);

                int c = a * b;
                dr["L3"] = c.ToString();
            }
        }

        GridView1.DataSource = Dset;
        GridView1.DataBind();
    }


Best of luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900