Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Gridview data bond twice

I defined -
<asp:GridView ID="GridView1" runat="server" BackColor="#FFFFFF"
                    ForeColor="#5078B3" AlternatingRowStyle-BackColor="#F0F0F0" >

In C#, I have the following and the gridview is filled with data.
            rdr = cmd.ExecuteReader();
            GridView1.DataSource = rdr;

Now I want add Select command button on the first column of each row. I added as following -

<asp:GridView ID="GridView1" runat="server" BackColor="#FFFFFF"
                    ForeColor="#5078B3" AlternatingRowStyle-BackColor="#F0F0F0"
                    <asp:CommandField HeaderText="Select" ShowSelectButton="True"/>
                    <asp:BoundField DataField="itemId" HeaderText="Item ID"/>
                    <asp:BoundField DataField="Equipment" HeaderText="Equipment" />
                    <asp:BoundField DataField = .......
               <headerstyle .....
In C#, I added -
protected void GridView1_DataBound(object sender, EventArgs e)
        GridView1.HeaderRow.Cells[0].Width = new Unit(80, UnitType.Pixel);
        GridView1.HeaderRow.Cells[1].Width = new Unit(80, UnitType.Pixel);
        GridView1.HeaderRow.Cells[2].Width = new Unit(100, UnitType.Pixel);
        GridView1.HeaderRow.Cells[3].Width = new Unit(100, UnitType.Pixel);
        foreach (TableRow row in GridView1.Rows)
            row.Cells[0].Width = new Unit(80, UnitType.Pixel);
            row.Cells[1].Width = new Unit(80, UnitType.Pixel);
            row.Cells[2].Width = new Unit(100, UnitType.Pixel);
            row.Cells[3].Width = new Unit(100, UnitType.Pixel);

Now I got two sets of data - one set with Select button and one without.
How can I get rid of the one set that does not have the Select command button? I still need to set   rdr = cmd.ExecuteReader(); GridView1.DataSource = rdr; GridView1.DataBind(); right?


1 Solution

Did you have the AutoGenerateColumns property on? or when you said 2 sets of data you men 2 diferente set of rows?
Paul JacksonCommented:
You shouldn't need the databind when you set the datasource it will databind automatically.
What is the property value set for page directive of AutoEventWireup  
If it is true event will trigger implicitly.  

 @ Page language="c#" Codebehind="WebForm1.aspx.cs"
         AutoEventWireup="false" Inherits="TestWebApp.WebForm1"
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

minglelinchAuthor Commented:
Thanks for the comments. I'll get back here.
minglelinchAuthor Commented:
I have set AutoGenerateColumns to true. This is the problem. After I set it to false, only one set of data shows. Thanks for all the comments.
minglelinchAuthor Commented:
good comment.

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now