Question

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.

Était-ce utile?

La solution

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

Autres conseils

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.

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"]);                        
}
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;

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top