Solved

Gridview data bond twice

Posted on 2011-03-09
6
422 Views
Last Modified: 2012-05-11
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.

0
Comment
Question by:minglelinch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35084214
Hi,

Did you have the AutoGenerateColumns property on? or when you said 2 sets of data you men 2 diferente set of rows?
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35084227
You shouldn't need the databind when you set the datasource it will databind automatically.
0
 
LVL 5

Expert Comment

by:jijeesh
ID: 35084386
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"
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 1

Author Comment

by:minglelinch
ID: 35206440
Thanks for the comments. I'll get back here.
0
 
LVL 1

Author Comment

by:minglelinch
ID: 35235159
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.
0
 
LVL 1

Author Closing Comment

by:minglelinch
ID: 35235164
good comment.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question