Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
Hi,

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

.aspx:-

XML
<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:---
C#
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
Updated 31-Aug-13 0:53am
v2

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".
 
Share this answer
 
set auto generate column="false"
 
Share this answer
 
set
<asp:gridview id="GridView1" runat="server" autogeneratecolumn="false">
 
Share this answer
 
v3
try this
C#
<asp:gridview id="GridView1" runat="server" autogeneratecolumn="false" >

</asp:gridview>


it helps u
 
Share this answer
 
v2
Comments
rupai99 31-Aug-13 14:00pm    
Many many thanks to you all....
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
 
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