C# 如何在代码隐藏中为datagridviewtextboxcolumn赋值?
我有一个datagridviewtextbox column类型的gridview 它有以下字段C# 如何在代码隐藏中为datagridviewtextboxcolumn赋值?,c#,gridview,desktop-application,assign,datagridviewtextboxcell,C#,Gridview,Desktop Application,Assign,Datagridviewtextboxcell,我有一个datagridviewtextbox column类型的gridview 它有以下字段 SrNo. | Description | HSN Code | Qty | Rate | Amount 我在数据集中获取了“描述”、“HSN代码”、“数量”和“费率”的记录 我想在我的程序中生成“SrNo”和“Amount” 我的代码是: int i=0; ds = new DataSet(); ds = db.getDetailRecords(Convert.ToI
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"]);
}
inti=0;
ds=新数据集();
ds=db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));
对于(i=0;i
但它不起作用。它给出了索引超出范围的错误。
如何将数据集值分配给网格?
请提供帮助。数据集表是用这两列“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"]);
}
inti=0;
ds=新数据集();
ds=db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));
//加上这个
grdData.DataSource=ds.Tables[0];
对于(i=0;i
确保
SrNo
和Amount
分别是数据源中的第0列和第5列。我猜您跳过了数据绑定到GridView
:
int i=0;
ds = new DataSet();
ds = db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));
grdData.DataSource = ds.Tables[0]; // you skipped this
请尝试以下代码:
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"]);
}
inti=0;
ds=新数据集();
ds=db.getDetailRecords(Convert.ToInt32(txtBillNo.Text));
grdData.Rows.Clear()
对于(i=0;i
用于(i=0;i
因为dgv作为datagridview索引从0开始,ds作为dataset也从0开始
自动生成的列应为false 那么您在datagridview
中定义了列了吗?然后您必须向其中添加一行,因为网格中没有任何行(我假设您没有绑定任何内容到网格),如grdData.rows.add()
Yes网格列是以前定义的。我不知道如何添加新行?请你提供一些代码好吗@V4VendettaPlease显示更多代码,您如何初始化网格视图的代码??是的,先生,我已检查了数据集,记录已成功返回。但我不知道如何使用“栅格视图”行@康拉德·克拉克汉克斯先生,数据显示正确。。但是出现了一个问题,列显示了两次@Praveen Nambiarth网格的预定义列以及数据集中的列也会显示出来。设置AutoGenerateColumns=“false”
,因为它总是完美地工作。。。非常感谢你,先生@Praveen NambiarSir我需要更多的帮助。请看我的这个[问题][@Praveen NambiarI不知道以这种方式添加新行时如何创建单元格。也许它会给你另一个索引超出范围。
因为单元格?@ConradClark你怎么能这样说?这不会给OP任何错误。
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