Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public void fieldsgetdata() //using method to retrive data
       {
           SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ESS"].ConnectionString);//VED_ID=(SELECT max(VED_ID)from Vehicle_Expense_Detail )
           conn.Open();
           SqlDataAdapter da = new SqlDataAdapter("SELECT EXP_EXPENSEHEAD_ID as 'Expense ID',VED_QTY as 'Quantity',VED_RATE as 'Rate',VED_AMOUNT as 'Amount',VED_REMARKS as 'Remarks' ,VED_NARRATION as 'Narration' FROM Vehicle_Expense_Detail WHERE VEM_ID=(SELECT max(VEM_ID)from Vehicle_Expense_Detail); ", conn);
           DataTable dt = new DataTable();
           da.Fill(dt);





           for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
           {

               dataGridView1.Rows[i].Cells[0].Value = dt.Rows[0][0].ToString();
               dataGridView1.Rows[i].Cells[0].Value = dt.Rows[1][0].ToString();






           }

       }


What I have tried:

<pre> public void fieldsgetdata() //using method to retrive data
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ESS"].ConnectionString);//VED_ID=(SELECT max(VED_ID)from Vehicle_Expense_Detail )
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT EXP_EXPENSEHEAD_ID as 'Expense ID',VED_QTY as 'Quantity',VED_RATE as 'Rate',VED_AMOUNT as 'Amount',VED_REMARKS as 'Remarks' ,VED_NARRATION as 'Narration' FROM Vehicle_Expense_Detail WHERE VEM_ID=(SELECT max(VEM_ID)from Vehicle_Expense_Detail); ", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
           
           



            for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
            {
               
                dataGridView1.Rows[i].Cells[0].Value = dt.Rows[0][0].ToString();
                dataGridView1.Rows[i].Cells[0].Value = dt.Rows[1][0].ToString();






            }

        } 
Posted
Updated 1-Nov-19 0:09am

1 solution

The recommended way is to use binding, an example:
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 1-Nov-19 7:10am    
Binding is a complex topic for starters, but it is always the best.

5ed.

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