Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):

C#
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) :
XML
<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
Updated 17-Feb-11 7:37am
v2
Comments
Vigneshb6 1-Jan-11 4:09am    
Only ur code should be placed in the code block.

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();



C#
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();
    }
 
Share this answer
 
First you have to give unique dataField for every column, in your code you did not.

XML
<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[^]
 
Share this answer
 
Comments
AnupKumarYadav 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
Hai Use this Example:
XML
<asp:GridView ID="grdview" runat="server" AutoGenerateColumns="false">
   <Columns>
   <asp:BoundField HeaderText="Name" DataField="name" />
   <asp:BoundField HeaderText="Address" DataField="address" />
   </Columns>
   </asp:GridView>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900