locked
Visual Webpart GridView with DropDownList RRS feed

  • Question

  • Hi,

    I have a visual webpart that contains a GridView and a "Add Row" button. I'm using the GridView for data entry and the number of rows are not fixed.

    I'm using DataTable with the required columns and an empty row and then bind to GridView when PageLoad. When user click on "Add Row" button, another row will be added.

    In my GridView, I have 2 columns, col1, col2. I would like to display a dropdownlist with data for col2 for user to pick from. And the data for the dropdownlist is from a sharepoint list.

    May I know how to bind the dropdownlist to its datasource?

    <asp:GridView ID="gvDetail" runat="server"  CellPadding="4" EnableModelValidation="True"  ForeColor="#333333" GridLines="None" autogeneratecolumns="False">
                    
    <Columns>
       <asp:TemplateField HeaderText="No">
       <ItemTemplate>
         <asp:TextBox ID="No" runat="server" Text=''></asp:TextBox>
       </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Group">
       <ItemTemplate>
          <asp:DropDownList ID="MatGrp" runat="server">
          </asp:DropDownList>
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>               
    </asp:GridView>
                
    <asp:Button ID="AddNewRow" runat="server" Text="Add New Row" onclick="AddNewRow_Click" />
    dt = new DataTable("Detail");
    dc = new DataColumn("No", Type.GetType("System.String"));
    dt.Columns.Add(dc);
    dr = dt.NewRow();
    dr["No"] = no;
    dt.Rows.Add(dr);
    gvDetail.DataSource = dt;
    gvDetail.DataBind();



    • Edited by zlgoay Thursday, June 6, 2013 8:29 AM
    Thursday, June 6, 2013 8:27 AM

Answers

  • Hi,

    In that case, you should place the code in "RowEditing" (or) some other event and bind the dropdown with the values. This code is for fetching the values.

    You can either fetch the values from sharepoint list and store in view state and bind those values (or) call the same code for every row editing, its upto you. This piece of code is for getting values from a particular splist, place it in that event where you want to bind it to drop down:

    using (SPSite site = new ("http://yoursharepointsite"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["NameOfYourList"];
                        MatGrp.DataSource = list.Items;
                        MatGrp.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
                        MatGrp.DataTextField = "Title"; // List field holding name to be displayed on page 
                        MatGrp.DataBind();
                    }
                }

    Please refer following link, for binding dropdown which resides inside gridview:

    http://stackoverflow.com/questions/6922142/databinding-dropdownlist-inside-gridview-edititemtemplate


    Please don't forget to 'mark answer/propose answer' or 'vote as helpful' as appropriate.



    • Edited by Mahesh.Nalam Thursday, June 6, 2013 9:21 AM
    • Marked as answer by Emir Liu Thursday, June 13, 2013 2:34 AM
    Thursday, June 6, 2013 9:19 AM

All replies

  • Hi,

    You can get some data and bind it to a drop down list something like this:

    using (SPSite site = new ("http://yoursharepointsite"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["NameOfYourList"];
                        MatGrp.DataSource = list.Items;
                        MatGrp.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
                        MatGrp.DataTextField = "Title"; // List field holding name to be displayed on page 
                        MatGrp.DataBind();
                    }
                }

    Please go through this link for further details: http://blogs.msdn.com/b/mattlind/archive/2008/02/12/bind-a-asp-dropdownlist-to-a-sharepoint-list.aspx

    Please don't forget to 'mark answer/propose answer' or 'vote as helpful' as appropriate.


    Thursday, June 6, 2013 8:49 AM
  • Hi Mahesh,

    In Visual Studio PageLoad Method, PRDMatGrp (dropdownlist) is not available, I can only put gvDetail. Besides, my dropdownlist is in GridView.

    regards,

    zl

    Thursday, June 6, 2013 8:58 AM
  • Hi,

    In that case, you should place the code in "RowEditing" (or) some other event and bind the dropdown with the values. This code is for fetching the values.

    You can either fetch the values from sharepoint list and store in view state and bind those values (or) call the same code for every row editing, its upto you. This piece of code is for getting values from a particular splist, place it in that event where you want to bind it to drop down:

    using (SPSite site = new ("http://yoursharepointsite"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["NameOfYourList"];
                        MatGrp.DataSource = list.Items;
                        MatGrp.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
                        MatGrp.DataTextField = "Title"; // List field holding name to be displayed on page 
                        MatGrp.DataBind();
                    }
                }

    Please refer following link, for binding dropdown which resides inside gridview:

    http://stackoverflow.com/questions/6922142/databinding-dropdownlist-inside-gridview-edititemtemplate


    Please don't forget to 'mark answer/propose answer' or 'vote as helpful' as appropriate.



    • Edited by Mahesh.Nalam Thursday, June 6, 2013 9:21 AM
    • Marked as answer by Emir Liu Thursday, June 13, 2013 2:34 AM
    Thursday, June 6, 2013 9:19 AM