Click here to Skip to main content
15,899,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have a gridview which show datas from sql, i wrote code for it who run it load datas from sql to gridview and it worked truly but after i fix header for it, it couldn't show some of columns. i want when i select a row it show me into textbox but i get error for cells are empty.
Could you help me?
C#
private void ContactGrid_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
       {
           if (ContactGrid.SelectedRows.Count == 1)
           {
               int index = e.Row.Index;
               txtContactId.Text = ContactGrid.Rows[index].Cells[0].Value.ToString();
               txtContactName.Text = ContactGrid.Rows[index].Cells[1].Value.ToString();
               txtContactFamily.Text = ContactGrid.Rows[index].Cells[2].Value.ToString();
               txtContactAddress.Text = ContactGrid.Rows[index].Cells[3].Value.ToString();
               txtContactPhoneNumber.Text = ContactGrid.Rows[index].Cells[4].Value.ToString();
               txtContactBirthDate.Text = ContactGrid.Rows[index].Cells[5].Value.ToString();
               txtContactEmailAddress.Text = ContactGrid.Rows[index].Cells[6].Value.ToString();
               txtContactFasebookAddress.Text = ContactGrid.Rows[index].Cells[7].Value.ToString();

           }

cells from address and facebook are empty. and i get error for these.
Posted

1 solution

Remove the Value property :

C#
ContactGrid.Rows[index].Cells[3].ToString();


The value in your database is null (or DbNull) on which you cannot perform a ToString()
 
Share this answer
 
Comments
Elham.Deljooei 25-Sep-13 3:11am    
Hi friend,
Why cell address can't fill from sql?
Er Daljeet Singh 25-Sep-13 3:58am    
How you are binding the data with gridview.You are binding the data manually or using the SqlDataSourceControl

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