0

I have a gridview of type datagridviewtextbox column.

It has following fields.

SrNo.    | Description    | HSN Code    | Qty   | Rate   | Amount 

I have fetched records of "Description", "HSN Code" , "Qty" & "Rate" in the dataset.

I Want to generate the "SrNo" and "Amount" in my program.

My code is:

int i=0;
ds = new DataSet();
ds = db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));


for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
     grdData.Rows[i].Cells[0].Value = i;
     grdData.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["Description"].ToString();
     grdData.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["HSNCode"].ToString();
     grdData.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["Qty"].ToString();
     grdData.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["Rate"].ToString();
     grdData.Rows[i].Cells[5].Value = Convert.ToDouble(ds.Tables[0].Rows[i]["Qty"]) * Convert.ToDouble(ds.Tables[0].Rows[i]["Rate"]);                        
}

But it's not working. It gives the error that Index was out of Range.

How do I assign the dataset values to the grid? Please help.

Mohemmad K
  • 809
  • 7
  • 33
  • 74
  • Well have you defined the columns in the `datagridview` ? then you will have to add a row to it coz the grid won't have any rows (i assume you are not binding anything to the grid) like `grdData.Rows.Add()` – V4Vendetta Apr 09 '13 at 12:11
  • Yes the grid columns are previously defined. I don't know how to work with adding new row? Will you please provide some code? @V4Vendetta – Mohemmad K Apr 09 '13 at 12:14
  • Please show more code, the code how you intialized the grid view?? – Dolo Apr 09 '13 at 12:21

4 Answers4

1

Is the dataset table created with these two columns "SrNo" and "Amount"?

If not, that's the reason you're getting that exception. I know you want to generate them on the fly but to access the fields like that they must at least be present in the dataset's table, namely ds.Tables[0].

Be sure db.getDetailRecords returns a valid DataSet for the columns you're asking for.

Oh, plus the datagridview doesn't have any rows. I suggest you bind it to your dataset before changing anything, you can do that by setting the DataGridView's DataSource property.

int i=0;
ds = new DataSet();
ds = db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));

//add this
grdData.DataSource = ds.Tables[0];

for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
     grdData.Rows[i].Cells[0].Value = i;
     //You don't need to set the other properties, they were binded when you put the DataSource in there
     grdData.Rows[i].Cells[5].Value = Convert.ToDouble(ds.Tables[0].Rows[i]["Qty"]) * Convert.ToDouble(ds.Tables[0].Rows[i]["Rate"]);                        
}

Be sure that SrNo and Amount are respectively the columns 0 and 5 in your DataSource.

Conrad Clark
  • 4,533
  • 5
  • 45
  • 70
  • Yes sir,I have checked the dataset and the records are successfully returned. But I don't know how to work with the grid view row.. @Conrad CLark – Mohemmad K Apr 09 '13 at 12:19
1

I guess you skipped the DataBinding to GridView:

int i=0;
ds = new DataSet();
ds = db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));
grdData.DataSource = ds.Tables[0]; // you skipped this
Praveen Nambiar
  • 4,852
  • 1
  • 22
  • 31
0

Try this Code:

int i=0;
ds = new DataSet();
ds = db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));

grdData.Rows.Clear()
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
     grdData.Rows.Add(); /// For add a Row. then does not show index out of range error
     grdData.Rows[i].Cells[0].Value = i;
     grdData.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["Description"].ToString();
     grdData.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["HSNCode"].ToString();
     grdData.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["Qty"].ToString();
     grdData.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["Rate"].ToString();
     grdData.Rows[i].Cells[5].Value = Convert.ToDouble(ds.Tables[0].Rows[i]["Qty"]) * Convert.ToDouble(ds.Tables[0].Rows[i]["Rate"]);                        
}
Sathish
  • 4,419
  • 4
  • 30
  • 59
  • I don't know how cells are created when you add a new row this way. Maybe it will give you another `Index was out of Range.` because of the cells? – Conrad Clark Apr 09 '13 at 12:25
  • @ConradClark How can you tell like this.? This wont give OP any error. – Sathish Apr 09 '13 at 12:28
0
for (i = 0; i < ds.Tables[0].Rows.Count; i++)

use for that (i = 0; i == ds.Tables[0].Rows.Count; i++)
//  not show index out of range

Because dgv as datagridview index starts from 0 and ds as dataset also from 0.

Autogenerated column should be false;

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459