C# 3 DataGridView链接问题
我的windows窗体中有三个DataGridView。一个是村庄的数据,单击任何单元格,第二个Datagridview将填充客户详细信息。单击客户名称后,第三个Datagridview必须填充并计算客户余额,无论是剩余信贷还是预付款,现在我面临的问题是第三个Datagridview 它特别抛出异常:对象引用设置为nullC# 3 DataGridView链接问题,c#,visual-studio-2010,datagridview,C#,Visual Studio 2010,Datagridview,我的windows窗体中有三个DataGridView。一个是村庄的数据,单击任何单元格,第二个Datagridview将填充客户详细信息。单击客户名称后,第三个Datagridview必须填充并计算客户余额,无论是剩余信贷还是预付款,现在我面临的问题是第三个Datagridview 它特别抛出异常:对象引用设置为null private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArg
private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
double loansum1 = 0;
double emisum1 = 0;
double dd1 = 0;
double ei1 = 0;
double finalsum = 0;
foreach (DataGridViewRow drr1 in dataGridView3.Rows)
{
if (drr1.Cells[1].Selected == true)
{
string customerid = drr1.Cells[0].Value.ToString();
label1.Text = customerid;
SqlConnection setcon = new SqlConnection(ConfigurationManager.ConnectionStrings["sismanager"].ConnectionString);
using (SqlCommand getsup = new SqlCommand("SELECT Ciid, Cdate, Cparticulars, Ccr, Cdr FROM Customerbills WHERE Cid = @Cid ORDER BY Cdate DESC", setcon))
{
getsup.Parameters.AddWithValue("@Cid", customerid);
SqlDataAdapter drrr = new SqlDataAdapter(getsup);
try
{
setcon.Open();
DataTable data1 = new DataTable();
drrr.Fill(data1);
if (data1.Rows.Count > 0)
{
dataGridView1.DataSource = data1;
dataGridView1.Columns[0].HeaderText = "Bill Number";
dataGridView1.Columns[1].HeaderText = "Bill Date";
dataGridView1.Columns[2].HeaderText = "Particulars";
dataGridView1.Columns[3].HeaderText = "Credit Balance";
dataGridView1.Columns[4].HeaderText = "Cash Balance";
for (int ij = 0; ij < (dataGridView1.Rows.Count); ++ij)
{
dd1 = Double.Parse(dataGridView1.Rows[ij].Cells[3].Value.ToString());
ei1 = Double.Parse(dataGridView1.Rows[ij].Cells[4].Value.ToString());
loansum1 += dd1;
emisum1 += ei1;
}
label4.Text = (Math.Round(loansum1)).ToString();
label5.Text = (Math.Round(emisum1)).ToString();
finalsum = (emisum1 - loansum1);
if (finalsum >= 0)
{
label6.Text = "Rs. " + finalsum + " (Previous Amount Clear)";
}
else
{
label6.Text = "Rs. " + finalsum + " (Previous Amount Due)";
}
}
}
catch (SqlException exx)
{
}
finally
{
setcon.Close();
}
}
}
}
}
首先使用Ciid作为票据编号,将标题更改为实名。第二,列类型与数据库相同。所以,如果数据库已经将dd1作为一个数字,则无需解析。如果它是一个字符串,那么就不需要使用Value。ToString()只需要Value。当您执行Value.ToString()时,您会在网络中得到类型名“string”。先生,我尝试将其从:dd1=Double.Parse(dataGridView1.Rows[ij].Cells[3].Value.ToString()更改为:;to dd1=Double.Parse(dataGridView1.Rows[ij].Cells[3].Value);但它现在是红色的,显示解析错误。然而,当我用类似的代码在其他应用程序中运行它时,它会正常工作。但这是一个问题。我也比较了代码。比较数据库中变量的类型。类型不同。类似于
dataGridView1.Rows[ij]。Cells[3]。Value
为null。您需要了解它为什么为null,并确保它已填充或检查它是否为null,然后再尝试将其解析为doubleGreat先生,我之前无法解释该问题。我在循环中使用了if子句,并将其设置为检查单元格中变量的空值,如果为空,则将其设置为0.0。问题就如你们说的那样解决了。
dd1 = Double.Parse(dataGridView1.Rows[ij].Cells[3].Value.ToString());