SPGridVew- How to get selected row when using DataSourceID?
Hi everyone.
I try to create a SPGridView with a ButtonFiled that return selected Row.
As I want to use filtering and sorting, I fill my datagrid with an ObjectDataSource whose ID match with my DataGrid DataSourceID. I’m not filling it with the datagrid.datasource property.The datagrid is fairly generated, sorting and filtering functions are just fine.
//vue is a SPgridView previously Declared;
ObjectDataSource gridDS = new ObjectDataSource();
//GenerateColumns();
gridDS.ID = DATASOURCEID;
gridDS.SelectMethod = "SelectData"; //Private function that return a datatable
//Sending some parameteres to the SelectMethod
gridDS.SelectParameters.Add("LdapProperties", LdapProperties);
gridDS.SelectParameters.Add("LdapQuery", LdapQuery);
gridDS.SelectParameters.Add("LdapLogin", LdapLogin);
gridDS.SelectParameters.Add("LdapPassword", LdapPassword);
gridDS.SelectParameters.Add("LdapDomain", LdapDomain);
gridDS.TypeName = this.GetType().AssemblyQualifiedName;
gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
//Event Selection
vue.RowCommand += new GridViewCommandEventHandler(vue_RowCommand);
//vue is a SPgridView previously Declared;
Controls.Add(gridDS);
vue.DataSourceID = gridDS.ID;
// Paging
vue.AllowPaging = true;
vue.PageSize = 5;
// Sorting
vue.AllowSorting = true;
// Filtering
vue.AllowFiltering = true;
Controls.Add(vue);
SPGridViewPager pager = new SPGridViewPager();
pager.GridViewId = vue.ID;
Controls.Add(pager);
The point is that when my rowCommand event handler is solicited, the datagrid seems empty. There is no row.
void vue_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
if (vue.Rows.Count > 0) // In debug vue.Rows.Count is always =0
{
GridViewRow selectedRow = vue.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
}
I tried to recall a datagrid.databind() in the event handler, but datagrid.rows.count is still =0;
How can I get the selected row? Does using a ButtonField is a good choice?
Answers
- No good or bad choice over a buttonfield.
I don't know where you register this eventhandler. But you should try to hookup the EventHandler and add it to the Control tree in CreateChildControls () instead.
Similar post:
SPGridView, RowCommand event in a connectable web part(http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/58c17789-f542-4e61-8cc9-cca59daf9b89/)
Keep It Simple and Stupid.- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM
All Replies
- No good or bad choice over a buttonfield.
I don't know where you register this eventhandler. But you should try to hookup the EventHandler and add it to the Control tree in CreateChildControls () instead.
Similar post:
SPGridView, RowCommand event in a connectable web part(http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/58c17789-f542-4e61-8cc9-cca59daf9b89/)
Keep It Simple and Stupid.- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM


