Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datagridview with name GRIDVIEWITEM_BILL and have six cell(column)
1(Item)(combobox)
2(Qty)(textbox)
3(Amount)(textbox)
4(Discount)(textbox)
5(Tax)(textbox)
6(Total)(textbox)
i want to fill on load of form from sql
my code
C#
public void billgridfill()
        {
            
            int i = 0;
            string str = "select Item,Qty,Amount,Discount,Tax,Total from Bill where Opd_No like'" + TBBOX_OPDNO_BILL.Text + "' And F_Name like'" + TBBOX_PNAME_BILL.Text + "'";
            SqlCommand cmd = new SqlCommand(str, Con_Class.con);
            c.con_open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while(sdr.Read())
            {
                GRIDVIEWITEM_BILL.Rows[i].Cells[0].Value = sdr["Item"].ToString();
                GRIDVIEWITEM_BILL.Rows[i].Cells[1].Value = sdr["Qty"].ToString();
                GRIDVIEWITEM_BILL.Rows[i].Cells[2].Value = sdr["Amount"].ToString();
                GRIDVIEWITEM_BILL.Rows[i].Cells[3].Value = sdr["Discount"].ToString();
                GRIDVIEWITEM_BILL.Rows[i].Cells[4].Value = sdr["Tax"].ToString();
                GRIDVIEWITEM_BILL.Rows[i].Cells[5].Value = sdr["Total"].ToString();
                GRIDVIEWITEM_BILL.Rows.Add(row);
                i++;
                
            }
            sdr.Close();
            c.con_close();
        }
Posted
Updated 19-Jan-14 23:35pm
v2

 
Share this answer
 
Use this:

C#
if(sdr.HasRows)
{
        GRIDVIEWITEM_BILL.DataSource = dr;
        GRIDVIEWITEM_BILL.DataBind();
}


OR

C#
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da = new SqlDataAdapter(sql,ConnectionString);
da.Fill(dt);
GRIDVIEWITEM_BILL.DataSource = dr;
GRIDVIEWITEM_BILL.DataBind();
 
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