Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

C#
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;
    }

}
Posted
Comments
Naz_Firdouse 7-Apr-14 5:22am    
what is the exception you are getting?
Jamie888 7-Apr-14 5:25am    
now the exception is gone, maybe some bugs which caused the exception. But the newID value is still null. What is the problem?
Naz_Firdouse 7-Apr-14 5:26am    
make sure the data you binded is not null...
Jamie888 7-Apr-14 5:30am    
ya, it is not null, from the nRowIndex, i get 1 as i only has only 1 row of record in my datagrid. Then i pass it into the second line of codes. By right it should giv me the value of the cell which is "2", but it will only return null.

C#
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;
    }
}
 
Share this answer
 
v3
Comments
Jamie888 7-Apr-14 5:39am    
there is no .Text extension for the dtgFTP.Rows[nRowIndex].Cells[1]
lukeer 7-Apr-14 5:52am    
DataGridViewCell[^] has not Text property. But Value[^] does exist. Please explain your suggestion.
PJ003 7-Apr-14 5:59am    
If you are using DataGridView, how can you use Rows.Count ???
Jamie888 8-Apr-14 5:22am    
its already been stated on the title. Anyway thanks for your effort. The problem has been solved.
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();
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900