Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am trying to populate data in datagridview which also contains datagridviewcombobox and i used stored procedure to fetch data from the stored procedure.

My code is

C#
void Show_customer_bills_Details()
        {
            connection();
            SqlCommand cmd = new SqlCommand("Bill_Search_Details", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@invno", editdetails);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            DataRow dr = dt.NewRow();
            //con.Close();
            int count = dt.Rows.Count;

            for (int i=0;i<count;i++)
 {
="" dt.importrow(dr);
="" grdsearch.rows[i].cells["item_description"].value="dt.Rows[i]["Item_Description"].ToString();
" grdsearch.rows[i].cells["hsn_code"].value="dt.Rows[i]["HSN_Code"].ToString();
" grdsearch.rows[i].cells["quantity"].value="dt.Rows[i]["Quantity"].ToString();
" grdsearch.rows[i].cells["converstion_type"].value="dt.Rows[i]["Converstion_Type"].ToString();
" grdsearch.rows[i].cells["rate"].value="dt.Rows[i]["Rate"].ToString();
" grdsearch.rows[i].cells["amount"].value="dt.Rows[i]["Amount"].ToString();
" grdsearch.rows[i].cells["unit_type"].value="dt.Rows[i]["Unit_Type"].ToString();
" }<="" b="">


AND I AM GETTING ERROR

'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'


What I have tried:

earlier i was using
C#
dt.Rows.Add(dr);

i recieved an error
'This row already belongs to this table.'

then i tried
C#
dt.ImportRow(dr);

i recieved an error :
'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'


please help me...
Posted
Updated 5-Jun-19 23:11pm
v4

C#
adp.Fill(dt);

You should check the return value from the call to Fill, to see if any rows were returned.
 
Share this answer
 
Comments
MukulMohal 6-Jun-19 4:11am    
yes i did checked, there were two rows that were returned
Richard MacCutchan 6-Jun-19 4:30am    
Then you need to use your debugger to see exactly where the error occurs and which index value is out of range.
MukulMohal 6-Jun-19 5:02am    
I was able to get the data from the database using

for (int i = 0; i < count; i++)
{
//dt.Rows.Add(dr[i]);
DataGridViewRow row = grdsearch.Rows[grdsearch.Rows.Add()];
grdsearch.Rows[i].Cells["Item_Description"].Value = dt.Rows[i]["Item_Description"].ToString();
grdsearch.Rows[i].Cells["HSN_Code"].Value = dt.Rows[i]["HSN_Code"].ToString();
grdsearch.Rows[i].Cells["Quantity"].Value = dt.Rows[i]["Quantity"].ToString();
grdsearch.Rows[i].Cells["Converstion_Type"].Value = dt.Rows[i]["Converstion_Type"].ToString();
grdsearch.Rows[i].Cells["Rate"].Value = dt.Rows[i]["Rate"].ToString();
grdsearch.Rows[i].Cells["Amount"].Value = dt.Rows[i]["Amount"].ToString();
grdsearch.Rows[i].Cells["Unit_Type"].Value = dt.Rows[i]["Unit_Type"].ToString();
}

now in this there is problem that when ever i am populating the grid with new data it retain the previous data and increases row again and again plz help me in getting the data properly
Richard MacCutchan 6-Jun-19 5:20am    
Before you start adding data you need to remove the existing rows.
i was able to populate datagridview which consist of 2 combobox and 4 textbox.

grdsearch.DataSource = null;
grdsearch.Rows.Clear();
grdsearch.Refresh();

connection();
SqlCommand cmd = new SqlCommand("Bill_Search_Details", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@invno", editdetails);

SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
DataRow dr = dt.NewRow();
//con.Close();
int count = dt.Rows.Count;

//grdsearch.DataSource = dt;

for (int i = 0; i < count; i++)
{
//dt.Rows.Add(dr[i]);
DataGridViewRow row = grdsearch.Rows[grdsearch.Rows.Add()];
grdsearch.Rows[i].Cells["Item_Description"].Value = dt.Rows[i]["Item_Description"].ToString();
grdsearch.Rows[i].Cells["HSN_Code"].Value = dt.Rows[i]["HSN_Code"].ToString();
grdsearch.Rows[i].Cells["Quantity"].Value = dt.Rows[i]["Quantity"].ToString();
grdsearch.Rows[i].Cells["Converstion_Type"].Value = dt.Rows[i]["Converstion_Type"].ToString();
grdsearch.Rows[i].Cells["Rate"].Value = dt.Rows[i]["Rate"].ToString();
grdsearch.Rows[i].Cells["Amount"].Value = dt.Rows[i]["Amount"].ToString();
grdsearch.Rows[i].Cells["Unit_Type"].Value = dt.Rows[i]["Unit_Type"].ToString();
}


}
 
Share this answer
 
Comments
Richard MacCutchan 6-Jun-19 5:22am    
It would be much better (and easier) if you used DataBinding, rather than manually adding everything.
MukulMohal 6-Jun-19 5:48am    
can you please provide me the way to that.
Richard MacCutchan 6-Jun-19 6:00am    
Google for "C# databinding" and you will find all the information necessary.
MukulMohal 6-Jun-19 6:47am    
i tried that one also using

grdsearch.datasource=dt;

and also tried

grdsearch.datasource=bindingsource1;

bothe gave me empty rows only they dont display any data in the datagridview
Richard MacCutchan 6-Jun-19 7:56am    
Then you must have done something wrong. Look at DataGridView.DataSource Property (System.Windows.Forms) | Microsoft Docs[^] for sample code.

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