Solved

Gridview data bond twice

Posted on 2011-03-09
6
418 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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