获取datagridview中最后一行数据中的单元格值 [英] Get value of cell in last row of data in datagridview

查看:640
本文介绍了获取datagridview中最后一行数据中的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在函数调用时获取最后一行数据中单元格的值而不是用户单击?

newID将始终为null并且没有值传递给它并且会发生异常每次我运行它



How can i get the value of the cell in last row of data upon function calling and not user click?
the newID would always be null and no value is passed into it and exception would occur every time i run it

private void GetLastRow()
{
    if (dtgFTP.Rows.Count > 0)
    {
        int nRowIndex = dtgFTP.Rows.Count - 1; //last row index
        object newID = dtgFTP.Rows[nRowIndex].Cells[1].Value;
        int ID = Convert.ToInt32(newID) + 1;
        //int newID = nRowIndex + 1;
        txtftpID.Text = ID.ToString();
        txtftpID.ReadOnly = true;
    }
    else
    {
        txtftpID.Text = "1";
        txtftpID.ReadOnly = true;
    }

}

推荐答案

private void GetLastRow()
{
    if (dtgFTP.Rows.Count > 0)
    {
        int nRowIndex = dtgFTP.Rows.Count - 1; //last row index
        //replace dtgFTP.Rows[nRowIndex].Cells[1].Value to dtgFTP.Rows[nRowIndex].Cells[1].Text
        object newID = dtgFTP.Rows[nRowIndex].Cells[1].Text;
        int ID = Convert.ToInt32(newID) + 1;
        //int newID = nRowIndex + 1;
        txtftpID.Text = ID.ToString();
        txtftpID.ReadOnly = true;
    }
    else
    {
        txtftpID.Text = "1";
        txtftpID.ReadOnly = true;
    }
}


private void GetLastRow()
       {
           XmlReader reader = XmlReader.Create(XPathFolder + xmlFileFTP, new XmlReaderSettings());
           DataSet ds = new DataSet();

           ds.ReadXml(reader);
           dtgFTP.DataSource = ds.Tables["FTP"];

           if (dtgFTP.DataSource == null)
           {
               if (dtgFTP.Rows.Count > 0)
               {
                   int nRowIndex = dtgFTP.Rows.Count - 1; //last row index
                   object newID = dtgFTP.Rows[nRowIndex].Cells[0].Value;
                   int ID = Convert.ToInt32(newID) + 1;
                   //int newID = nRowIndex + 1;
                   txtftpID.Text = ID.ToString();
                   txtftpID.ReadOnly = true;
               }
           }
           else if (dtgFTP.DataSource != null)
           {
               if (dtgFTP.Rows.Count > 0)
               {
                   int nRowIndex = dtgFTP.Rows.Count - 2; //last row index
                   object newID = dtgFTP.Rows[nRowIndex].Cells[0].Value;
                   int ID = Convert.ToInt32(newID) + 1;
                   //int newID = nRowIndex + 1;
                   txtftpID.Text = ID.ToString();
                   txtftpID.ReadOnly = true;
               }
           }
           else
           {
               txtftpID.Text = "1";
               txtftpID.ReadOnly = true;
           }

           reader.Close();
       }


这篇关于获取datagridview中最后一行数据中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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