Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I am working on VS2005 wITH sql2005.
I have a simple gridview on a webpage.which loads the data from one of the tables from the database. It displays all the columns as follows(SECTION 1):
 
protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("initial catalog=shikhar;integrated security=yes;server=SHAMMI\\SQLEXPRESS");
        da = new SqlDataAdapter("Select id,name,city from rrr", conn);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
Now, i want the above scenario to behave diferently. What I need is that only two column (instead of three) should be displayed in datagrid using the following technique(SECTION 2) :
<div>
        <asp:GridView ID="GridView1" runat="server" Style="left: 336px; position: relative;top: 164px">
        <Columns>
        <asp:BoundField HeaderText="x" DataField="id" />
    <asp:BoundField HeaderText="y" DataField="id" />
        </Columns>
        </asp:GridView>
    </div>
My question is : what should i modify from Section 1 to achieve this.(or anything else that you might suggest). Since, when i added section 2 and run the webpage, it shows me all the section1 + section2 columns. And i want it to show only section2 columns.
Posted 31-Dec-10 21:22pm
Edited 17-Feb-11 7:37am
v2
Comments
Vigneshb6 at 1-Jan-11 4:09am
   
Only ur code should be placed in the code block.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
You should Disable auto column generation of the Gridview.
Either modify Gridview property in designer (AutoGenerateColumns="False")
for Example
<asp:GridView AutoGenerateColumns="False" ...>
 
or in the code editor
 
GridView1.AutoGenerateColumns=False ; //before calling DataBind();
 GridView1.DataBind();
 

protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("initial catalog=shikhar;integrated security=yes;server=SHAMMI\\SQLEXPRESS");
        da = new SqlDataAdapter("Select id,name,city from rrr", conn);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.AutoGenerateColumns = false ; 
        GridView1.DataBind();
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First you have to give unique dataField for every column, in your code you did not.
 
<div>
        <asp:GridView ID="GridView1" runat="server" Style="left: 336px; position: relative;top: 164px">
        <Columns>
        <asp:BoundField HeaderText="x" DataField="id" />
    <asp:BoundField HeaderText="y" DataField="name" />
        </Columns>
        </asp:GridView>
    </div>
 

Now make select comment like what you want to bind in column.
 
da = new SqlDataAdapter("Select id,name from rrr", conn);
 

I would like to go some basic understanding on datagrid from here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx[^]
  Permalink  
Comments
AnupKumarYadav at 1-Jan-11 4:07am
   
You can have as many column as you want with the same data field.
I Think the question is how to disable auto generation of Data Column when binded to a data source (Which is by Default True for GridView) - i.e AutoGenerateColumn =false
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hai Use this Example:
<asp:GridView ID="grdview" runat="server" AutoGenerateColumns="false">
   <Columns>
   <asp:BoundField HeaderText="Name" DataField="name" />
   <asp:BoundField HeaderText="Address" DataField="address" />
   </Columns>
   </asp:GridView>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 271
1 OriginalGriff 253
2 nv3 50
3 VJ Reddy 45
4 Mehdi Gholam 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 20 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100