Click here to Skip to main content
13,624,821 members
Rate this:
 
Please Sign up or sign in to vote.
Hi,

I want to bind my grid to database..Please find below my code:-

.aspx:-

<asp:GridView ID="GridView1" runat="server">
         <Columns>
                <asp:BoundField DataField="tran_date" HeaderText="Transaction Date"

                    SortExpression="tran_date" />
                <asp:BoundField DataField="RSQ" HeaderText="Transaction Cash"

                    SortExpression="RSQ" />
                <asp:BoundField DataField="RSU" HeaderText="Cash Unit"

                    SortExpression="RSU" />
                <asp:BoundField DataField="CSQ" HeaderText="Credit sale"

                    SortExpression="CSQ" />
                <asp:BoundField DataField="CSU" HeaderText="Credit Unit"

                    SortExpression="CSU" />
                     <asp:BoundField DataField="iAmount" HeaderText="Amount"

                    SortExpression="iAmount" />
            </Columns>
        </asp:GridView>


aspx.cs:---
public void fillgrid()
   {
       conns();
       SqlConnection con=new SqlConnection (conn);
       SqlCommand com;
       try
       {
           con.Open();
           com = new SqlCommand("usp_Dailysales_main", con);
           com.CommandType = CommandType.StoredProcedure;

           com.Parameters.AddWithValue("@FrmDate", DateTime.Parse(rdpFrom.SelectedDate.Value.ToString("yyyy/MM/dd"), CultureInfo.CreateSpecificCulture("en-CA")));
           com.Parameters.AddWithValue("@todate", DateTime.Parse(rdpTo.SelectedDate.Value.ToString("yyyy/MM/dd"), CultureInfo.CreateSpecificCulture("en-CA")));

           //com.Parameters.AddWithValue("@strfltr", strfltr1);
           //   com.Parameters.AddWithValue("@sptype", ddlsreturn.SelectedValue);
           com.Parameters.AddWithValue("@ch", Int32.Parse(Convert.ToString(ViewState["ch"])));

           SqlDataAdapter da = new SqlDataAdapter(com);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();


       }
       catch (Exception ex)
       {
       }

   }

The problem is, whenever I am calling fillgrid(), the grid columns are repeated twice. Is there any event where I can specify grid Data source...

Please help...
Posted 30-Aug-13 22:23pm
Updated 31-Aug-13 0:53am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Please refer this link you will understand the AutoGenerateColumn property.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx[^]

When you set AutoGenerateColumn="true" :
AutoGeneratedField object is automatically created for each field in the data source. Each field is then displayed as a column in the GridView control in the order that the fields appear in the data source.

When you creating column dynamically(as you created) you have to set AutoGenerateColumn="false".
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

set auto generate column="false"
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

set
<asp:gridview id="GridView1" runat="server" autogeneratecolumn="false">
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

try this
<asp:gridview id="GridView1" runat="server" autogeneratecolumn="false" >

</asp:gridview>


it helps u
  Permalink  
v2
Comments
rupai99 31-Aug-13 14:00pm
   
Many many thanks to you all....
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

Hi

You need to do the following stuff in your grid set Autogeneratedcolumn ="false"

<asp:gridview id="GridView1" runat="server" autogeneratecolumn="false" xmlns:asp="#unknown">

Thanks,
Mukesh
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180712.1 | Last Updated 29 Jan 2015
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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