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:
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 methodprivate 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!!!!