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?


Who is Participating?

Did you have the AutoGenerateColumns property on? or when you said 2 sets of data you men 2 diferente set of rows?
Paul JacksonSoftware EngineerCommented:
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"
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.