0

タイプ datagridviewtextbox 列のグリッドビューがあります。

以下のフィールドがあります。

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

データセットの「説明」、「HSN コード」、「数量」、「レート」のレコードを取得しました。

プログラムで「SrNo」と「Amount」を生成したい。

私のコードは次のとおりです。

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

しかし、それは機能していません。それはエラーを与えるIndex was out of Range.

データセット値をグリッドに割り当てるにはどうすればよいですか? 助けてください。

4

4 に答える 4

1

データセット テーブルは、この 2 つの列「SrNo」と「Amount」で作成されていますか?

そうでない場合は、その例外が発生する理由です。それらをその場で生成したいことは知っていますが、そのようなフィールドにアクセスするには、少なくともデータセットのテーブル、つまりds.Tables[0].

求めている列db.getDetailRecordsに対して有効な値を返すようにしてください。DataSet

ああ、さらに datagridview には行がありません。何かを変更する前にデータセットにバインドすることをお勧めします。DataGridView のDataSourceプロパティを設定することで実行できます。

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

SrNoAmountがそれぞれ DataSource の列 0 と 5 であることを確認してください。

于 2013-04-09T12:14:04.260 に答える
1

DataBindingtoをスキップしたと思いますGridView

int i=0;
ds = new DataSet();
ds = db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));
grdData.DataSource = ds.Tables[0]; // you skipped this
于 2013-04-09T12:17:38.943 に答える