Ask a questionAsk a question
 

AnswerSPGridVew- How to get selected row when using DataSourceID?

  • Wednesday, November 04, 2009 10:13 PMkerseub Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

All Replies