如何在C#windows应用程序中比较datagridview单元格值是空的 [英] How to compare datagridview cell value is empty in C# windows application

查看:52
本文介绍了如何在C#windows应用程序中比较datagridview单元格值是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个bankname组合框,acountno文本框,accounttype combobox,onedatagridview和一个Addbutton and Save按钮中有一个表单。

加载表单后如果我从组合框accountno和accounttype中选择bankname

框正在填写并在datagridview中显示相关的银行详细信息。添加按钮用于if select bankname然后我点击添加按钮新的空行添加在
$ b $的顶部b datagridview然后我输入数据到行和点击保存按钮。它保存数据库正常。现在我想要的是如果我添加新的新空行并单击保存按钮它将消息单元格值为空我试过不同的方法但它不行。请任何人

帮我解决这个问题。我得到编译时错误操作符!=无法应用int和system.dbnull错误的操作数



我尝试过:



I Have a form in that one bankname combobox,acountno textbox,accounttype combobox ,onedatagridview, and one Addbutton and Save button.
After Loading the form If i select bankname from the combobox accountno and accounttype
boxes are filling and showing the related bank details in datagridview fine.Add button is used for if select bankname and then i click add button new empty row is added top of the
datagridview and then i enter the data into row and the click save button.It saving database fine.Now what i want is if i add new new empty row and click save button it will message cell value is empty i tried different ways but it is not working .Please anybody
help me on this.I am getting compile time error operator != cannot be applied operands of the int and system.dbnull error

What I have tried:

<blockquote class="quote"><div class="op">Quote:</div><pre>
<blockquote class="quote"><div class="op">Quote:</div>  


       private void btnsave_Click(object sender, EventArgs e)
        {
            ChqMngrBsns.CheckBook chk = new ChqMngrBsns.CheckBook();

            // Inserting Each Record into database

            foreach (DataGridViewRow row in dgvwChqs.Rows)
            {
                if (bankid > 0)
                {
                    chk.bankid=bankid;


                    if (Convert.ToInt32(row.Cells["Slno"].Value) != DBNull.Value)
                    {

                        chk.Slno = Convert.ToInt32(row.Cells["Slno"].Value);

                    }

                    if (Convert.ToInt32(row.Cells["startno"].Value) != DBNull.Value)
                    {
                        chk.startingno = Convert.ToInt32(row.Cells["startno"].Value);
                    }

                    if (Convert.ToInt32(row.Cells["noofcheques"].Value) != DBNull.Value)
                    {
                        chk.Noofcheques = Convert.ToInt32(row.Cells["noofcheques"].Value);
                    }

                    if (Convert.ToInt32(row.Cells["endingno"].Value) != DBNull.Value)
                    {

                        chk.Endingno = Convert.ToInt32(row.Cells["endingno"].Value);
                    }

                    if (Convert.ToBoolean(row.Cells["Status"].Value == DBNull.Value))
                    {
                        chk.status = false;

                    }

                    else if (Convert.ToBoolean(row.Cells["Status"].Value != DBNull.Value))
                    {
                        chk.status = true;

                    }
                    chk.SaveData(true);

                }
            }
            //Clearing the datatable
            cmbbankname.SelectedIndex = 0;
            txtAcntno.Text = "";
            cmbAcntype.SelectedIndex = 0;
            dts.Clear();
            //LoadDataGridView();
        }</blockquote>

推荐答案

这样的事情会让你解决问题(警告 - 未经测试)

Something like this should get you over the problem (warning - untested)
int atry;
if (row.Cells["Slno"].Value != null)
{
    if (int.TryParse(row.Cells["Slno"].Value.ToString(), out atry)) 
    {
        chk.Slno = atry;
    }
}

if (row.Cells["startno"].Value != null)
{
    if (int.TryParse(row.Cells["startno"].Value.ToString(), out atry)) 
    {
        chk.startingno = atry;
    }
}

if (row.Cells["noofcheques"].Value != null)
{
    if (int.TryParse(row.Cells["noofcheques"].Value.ToString(), out atry)) 
    {
        chk.Noofcheques = atry;
    }
}

if (row.Cells["endingno"].Value != null)
{
    if (int.TryParse(row.Cells["endingno"].Value.ToString(), out atry)) 
    {
        chk.Endingno = atry;
    }
}


if (row.Cells["Status"].Value != null)
{
    bool abtry;
    if (bool.TryParse(row.Cells["Status"].Value.ToString(), out abtry))
    {
        chk.status = abtry;
    }
    else
    {
        chk.status = false;
    }
}


这篇关于如何在C#windows应用程序中比较datagridview单元格值是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆