This is a migrated thread and some comments may be shown as answers.

GridView: CurrentRowChanged Event

2 Answers 459 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gopal
Top achievements
Rank 1
Gopal asked on 26 Jun 2012, 09:00 AM
hello,
I am manually loading the GridView and in CurrentRowChanged event of the gridView i am trying to fill the set of textboxes and label control with data present in the selected row of the  GridView.
Here are the codes:
1. manually Loading GridView data in a method
 private void loadAllUsers()
        {
          
            var obj = _dataAccess.GetAllMembers();
            if (obj != null && obj.Count > 0)
            {
 
                gvListUsers.Visible = true;
 
                gvListUsers.Columns.Clear();
                gvListUsers.AutoGenerateColumns = false;
                gvListUsers.MultiSelect = false;
                gvListUsers.EnableGrouping = true;
                gvListUsers.EnableCustomGrouping = true;                
                gvListUsers.ShowGroupedColumns = true;
                
 
                gvListUsers.DataSource = obj;
 
                var textBoxColumn = new GridViewTextBoxColumn();
                textBoxColumn.Name = "TextBoxColumn";
                textBoxColumn.HeaderText = "ID";
                textBoxColumn.FieldName = "MembershipID";
                textBoxColumn.Width = 50;
                gvListUsers.MasterTemplate.Columns.Add(textBoxColumn);
 
 
                var txtColumnUsername = new GridViewTextBoxColumn();
                txtColumnUsername.Name = "txtBoxColumnUsername";
                txtColumnUsername.HeaderText = "UserName";
                txtColumnUsername.FieldName = "Username";
                txtColumnUsername.Width = 120;
                gvListUsers.MasterTemplate.Columns.Add(txtColumnUsername);
                             
                var textBoxColumnFirstname = new GridViewTextBoxColumn();
                textBoxColumnFirstname.Name = "TextBoxColumnFirstname";
                textBoxColumnFirstname.HeaderText = "Firstname";
                textBoxColumnFirstname.FieldName = "FirstName";
                textBoxColumnFirstname.Width = 120;
                gvListUsers.MasterTemplate.Columns.Add(textBoxColumnFirstname);
 
                var textBoxColumnMiddlename = new GridViewTextBoxColumn();
                textBoxColumnMiddlename.Name = "TextBoxColumnMiddlename";
                textBoxColumnMiddlename.HeaderText = "Middlename";
                textBoxColumnMiddlename.FieldName = "MiddleName";
                textBoxColumnMiddlename.Width = 120;
                gvListUsers.MasterTemplate.Columns.Add(textBoxColumnMiddlename);
 
                var textBoxColumnLastName = new GridViewTextBoxColumn();
                textBoxColumnLastName.Name = "TextBoxColumnLastName";
                textBoxColumnLastName.HeaderText = "Lastname";
                textBoxColumnLastName.FieldName = "LastName";
                textBoxColumnLastName.Width = 120;
                gvListUsers.MasterTemplate.Columns.Add(textBoxColumnLastName);
 
                var textBoxColumnIsActive = new GridViewTextBoxColumn();
                textBoxColumnIsActive.Name = "TextBoxColumnIsActive";
                textBoxColumnIsActive.HeaderText = "Active";
                textBoxColumnIsActive.FieldName = "IsActive";
                textBoxColumnIsActive.Width = 100;
                gvListUsers.MasterTemplate.Columns.Add(textBoxColumnIsActive);
                //column formating
 
               
            }
            else
            {
                gvListUsers.Visible = false;
            }
        }
2. CurrentRowChanged Event: using this event i am assigning the currentrow data of gridview to different textboxes and label control.
 private void gvListUsers_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            UpdateInfo(this.gvListUsers.CurrentRow);
        }
 
        private void UpdateInfo(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {
		//gettting error "Object reference not set to an instance of an object."
                this.lblMembershipID.Text = this.GetSafeString(currentRow.Cells["MembershipID"].Value);
		this.txtUsername.Text = this.GetSafeString(currentRow.Cells["Username"].Value);
            }
        }
3.
	private string GetSafeString(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            return value.ToString();
        }
4. i am getting "object reference not set to an instance of an object" error when am assigning currentrow data to the label control and for textbox too. I am unable to track it why i am getting this error. Kindly suggest me what can be done to resolve this issue. Thanks!!!!

2 Answers, 1 is accepted

Sort by
0
Accepted
Boryana
Telerik team
answered on 28 Jun 2012, 07:44 AM
Hi Gopal,

Thank you for contacting us.

Would you please change the MemberID to TextBoxColumn and UserName to txtBoxColumnUsername in the UpdateInfo method? The two lines should look like this:
this.lblMembershipID.Text = this.GetSafeString(currentRow.Cells["TextBoxColumn"].Value);
this.txtUsername.Text = this.GetSafeString(currentRow.Cells["txtBoxColumnUsername"].Value);

The exception is thrown because you are attempting to find a cell using the HeaderText of the column, not its Name. A bit aside, I would like to point out that it is better to set the DataSource property after you have added all columns to the MasterTemplate.

I hope this helps. Let me know if you have further queries.

Kind regards,
Boryana
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Gopal
Top achievements
Rank 1
answered on 28 Jun 2012, 11:38 AM
Boryana,
Thanks for the response.! Issued resolved. :)
Tags
GridView
Asked by
Gopal
Top achievements
Rank 1
Answers by
Boryana
Telerik team
Gopal
Top achievements
Rank 1
Share this question
or