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

Grid row selection....

4 Answers 82 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 04 Nov 2010, 10:44 AM
hey everyone

I am using a grid and template form for editing grid items.I have created a panel for editing items that is visible on external button btnEdit_Click.tha problem is with my code when i select a row and click on edit,i am trying to get the id of the row been selected.But its giving error-

Object reference not set to an instance of an object.

my code is--
protected void btnEdit_Click(object sender, EventArgs e)
       {
           if (RadGrid1.SelectedItems.Count != 0)
           {
               //Guid g = new Guid(txtName.Text);
               txtName.Focus();
               pnlExternalForm.Visible = true;
               RadGrid Grid = (this.FindControl("RadGrid1") as RadGrid);
               string UID = Convert.ToString(Grid.SelectedValues["UID"]);//this shows no row is selected
               string query = "SELECT * FROM Entry where UID=" + UID;
               GetProductInfoForEdit(query, UID);
               LoadData();
           }
       }
       private void GetProductInfoForEdit(string query, string UserID)
       {
           DataSet ds = GetInventoryForm(query);
           txtName.Text = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
           txtMailText.Text = Convert.ToString(ds.Tables[0].Rows[0]["Mail Text"]);
           txtMailSubject.Text = Convert.ToString(ds.Tables[0].Rows[0]["Mail Subject"]);
           lblUID.Text = UserID;
           LoadData();
       }
my primary key column is UID.

Thanks
Amit

4 Answers, 1 is accepted

Sort by
0
Amit
Top achievements
Rank 1
answered on 04 Nov 2010, 11:41 AM

with this i was using simple data binding,now i am using NeedDataSource event.But when i select a row and click edit button,this is false

if (RadGrid1.SelectedItems.Count != 0)

plz help?...
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Nov 2010, 12:09 PM
Hello Amit,

Check whether you have set the DataKeyNames property of MasterTableView to 'UID' and see if this eliminates the error. You can access the DataKeyNames using GetDataKeyValue() method. Sample code is given below.

ASPX:
<MasterTableView CommandItemDisplay="Top" DataKeyNames="UID">

C#:
protected void btnEdit_Click(object sender, EventArgs e)
   {
       if (RadGrid1.SelectedItems.Count != 0)
       {
           GridDataItem item = (GridDataItem)RadGrid1.SelectedItems[0];
           string id = item.GetDataKeyValue("UID").ToString(); // getting DataKeyValue of selected row
        }
   }

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 04 Nov 2010, 12:24 PM
thanks Princy,i was missing that.But as i told i used simple data binding before and now i have changed it to advance data  binding and using NeedDataSource event,Now,when i click button after selecting the row, the curson does'nt go inside this
if
(RadGrid1.SelectedItems.Count != 0)


What could be the problem?..
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Nov 2010, 01:00 PM
Hello Amit,

Could you please paste the complete code for futher help?

Or make sure the EnableViewState property of the grid is not set as False. Because the SelectedItems collection is updated on the server-side after a postback occurs. But when the ViewState of the control is disabled, the selected items count(client-side selection), would be equal to zero. Hope this information helps you.


Thanks,
Princy.
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Amit
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or