Link to home
Start Free TrialLog in
Avatar of minglelinch
minglelinch

asked on

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;
            GridView1.DataBind();

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"
                    OnDataBound="GridView1_DataBound">
           <Columns>                    
                    <asp:CommandField HeaderText="Select" ShowSelectButton="True"/>
                    <asp:BoundField DataField="itemId" HeaderText="Item ID"/>
                    <asp:BoundField DataField="Equipment" HeaderText="Equipment" />
                    <asp:BoundField DataField = .......
                    .....
               </Columns>
               <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?

Thanks.

ASKER CERTIFIED SOLUTION
Avatar of gamarrojgq
gamarrojgq

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paul Jackson
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.  

Eg:
 @ Page language="c#" Codebehind="WebForm1.aspx.cs"
         AutoEventWireup="false" Inherits="TestWebApp.WebForm1"
Avatar of minglelinch
minglelinch

ASKER

Thanks for the comments. I'll get back here.
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.
good comment.