Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
   {

       ddlCoName.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[2].Text;
       ddlCoName.DataValueField = gvGetAllBranchInfo.SelectedRow.Cells[3].Text;
       txtName.Text = gvGetAllBranchInfo.SelectedRow.Cells[4].Text;
       txtAddress1.Text = gvGetAllBranchInfo.SelectedRow.Cells[5].Text;
       txtAddress2.Text = gvGetAllBranchInfo.SelectedRow.Cells[6].Text;
       txtAddress3.Text = gvGetAllBranchInfo.SelectedRow.Cells[7].Text;
       txtMobile.Text = gvGetAllBranchInfo.SelectedRow.Cells[8].Text;
       txtEmail.Text = gvGetAllBranchInfo.SelectedRow.Cells[9].Text;
       ddlBranchTahsil.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[11].Text;
       ddlBranchTahsil.SelectedItem.Text = gvGetAllBranchInfo.SelectedRow.Cells[10].Text;
       ddlCityPost.DataValueField = gvGetAllBranchInfo.SelectedRow.Cells[12].Text;
       ddlCityPost.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[13].Text;
       txtPin.Text = gvGetAllBranchInfo.SelectedRow.Cells[14].Text;
       ddlDistrict.DataValueField = gvGetAllBranchInfo.SelectedRow.Cells[15].Text;
       ddlDistrict.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[16].Text;
       ddlState.DataValueField = gvGetAllBranchInfo.SelectedRow.Cells[17].Text;
       ddlState.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[18].Text;
}

I have write this code.but this is not properly work
I got this error as bellow

MSIL
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 112:txtEmail.Text = gvGetAllBranchInfo.SelectedRow.Cells[9].Text;
Line 113:ddlBranchTahsil.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[11].Text;
Line 114:ddlBranchTahsil.SelectedItem.Text = gvGetAllBranchInfo.SelectedRow.Cells[10].Text;
Line 115:ddlCityPost.DataValueField = gvGetAllBranchInfo.SelectedRow.Cells[12].Text;
Line 116:ddlCityPost.DataTextField = gvGetAllBranchInfo.SelectedRow.Cells[13].Text;
Posted
Updated 1-Jun-11 2:53am
v3
Comments
Timberbird 1-Jun-11 7:26am    
Obviously, either SelectedRow object, or its Cell[x] is null :). Put a breakpoint and check which one gives you that error
pankajupadhyay29 1-Jun-11 8:52am    
wrap the code in pre tag
pankajupadhyay29 1-Jun-11 8:55am    
not clear what are you doing where are these controls like ddlCoName???

1 solution

Hello Dear
working with DataGrid like you did is so hard,it is better that first you generate a datasource from that table by using 'Data -> Add New data Source' in VS . if table which you want to work on it is in a database , select Database from Window which displayed , if that is a class which you have generated in your code for example :
C#
public class Person
{
   public int PersonID
   {
     get;
     set;
   }
   public string PersonName
   {
     get;
     set;
   }
}


select object from Window which was displayed then next,... and VS create a datasource from that table or object or web service .
in your from add a BindingSource to your form and set its datasource that class which VS generated;
set datasource of your datagrid to BindingSource which you added to form and
set AutoGenerateColumns of DataGrid to True.
now if in your code you fill datasource of your BindingSource with list array of that generated class (for example Person) your datagrid will show all of data in BindingSource.DataSource

if you have some control in your form which you wants to show data of Current row in DataGid do this for example you have a TextBox that you want to show PersonId of Current row of DataGrid , Do this:
in designer of from select TextBox and in its property select DataBinding property and expand it , in text property which is displayed select BindingSource which you added to from and from it select property which you want textbox display it form example PersonId
now if you fill datasource of your BindingSource when you navigate in datagrid textbox displayes personid of current Row

Good luck
if you did these and had some problems , send me an E-mail and I will Send You A project which have all of them with describe
 
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