GridView: CurrentRowChanged Event

3 posts, 1 answers
  1. Gopal
    Gopal avatar
    20 posts
    Member since:
    Mar 2009

    Posted 26 Jun 2012 Link to this post

    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. Answer
    Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 28 Jun 2012 Link to this post

    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 >>
  3. Gopal
    Gopal avatar
    20 posts
    Member since:
    Mar 2009

    Posted 28 Jun 2012 Link to this post

    Boryana,
    Thanks for the response.! Issued resolved. :)
Back to Top