?
Solved

Gridview drop down list

Posted on 2008-06-19
2
Medium Priority
?
1,814 Views
Last Modified: 2008-06-30
I have a gridview displaying information from an access database. One field will be updateable. I want that field to be a dropdown list that displays data from a reference table. How can I get the gridview to show it's data along with the drop down list for that one feild? And once a user selects a different value from that drop down list, I want tthe update to the database to happen when the user makes the selection. Is there anyway to do this? Any help would be appreciated.
0
Comment
Question by:trav1514
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
MikeMCSD earned 1500 total points
ID: 21826048
<asp:GridView ID="grid1" . .
  OnRowDataBound="grid1_RowDataBound"  ...  >

<Columns>
<asp:TemplateField HeaderText="Move To">
<ItemTemplate>
<asp:DropDownList ID="ddlMoveTo" Width="98px" runat="server" OnSelectedIndexChanged="ddlMoveTo_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>


  protected void ddlMoveTo_SelectedIndexChanged(object sender, EventArgs e)
  {
    string giftID = string.Empty;
    string eventID = string.Empty;

    DropDownList ddl = (DropDownList)sender;
    GridViewRow row2 = (GridViewRow)ddl.NamingContainer;
    giftID = ((Label)row2.FindControl("lblGiftID")).Text;  // grids index value
    eventID = ddl.SelectedValue;    // value of selected item in drop down
    ............


To load the drop down:

  protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
   
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

      DropDownList ddlMoveTo = (DropDownList)e.Row.FindControl("ddlMoveTo");
      ddlMoveTo.DataSource = Session["table"]; // or from database
      ddlMoveTo.DataTextField = "EventName";
      ddlMoveTo.DataValueField = "EventID";
      ddlMoveTo.DataBind();
      ddlMoveTo.Items.Insert(0, new ListItem("Select an item:", "0"));
      ........




0
 

Author Comment

by:trav1514
ID: 21879379
I apologize for posting this question and not answering your response for so ,long. I got sidetraked by other things here at work. I finally got a chance to try this. I am still new to ASP.net and C#, so I'm not sure I followed completely. I've got this almost up, but of course my drop down list is not working. How ndo I connect to the db for it? I've posted my page code. Thanks for the help.

Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage2.aspx.cs" Inherits="TestPage2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Drop Down List Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="AccessDataSource1" EmptyDataText="There are no data records to display." OnRowDataBound="grid1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="LName" HeaderText="LName" SortExpression="LName" />
                <asp:BoundField DataField="FName" HeaderText="FName" SortExpression="FName" />
                <asp:TemplateField HeaderText="Position">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlPos" Width="98px" runat="server" OnSelectedIndexChanged="ddlPos_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\BaseballXP_Working.mdb"
            SelectCommand="SELECT ID, LName, FName, PosNo FROM CurrentBatterStats"
            UpdateCommand="UPDATE CurrentBatterStats SET LName = ?, FName = ?, PosNo = ? WHERE ID = ?">
            <UpdateParameters>
                <asp:Parameter Name="LName" Type="String" />
                <asp:Parameter Name="FName" Type="String" />
                <asp:Parameter Name="PosNo" Type="Double" />
            </UpdateParameters>
        </asp:AccessDataSource>
        &nbsp;
        <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="App_Data\BaseballXP_Working.mdb"
            SelectCommand="SELECT `ID`, `Position`, `Description` FROM `Positions`" >
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="ID" Type="Int32" />
                <asp:Parameter Name="Position" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Position" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>

Code Behind:
    protected void ddlPos_SelectedIndexChanged(object sender, EventArgs e)
    {
        string giftID = string.Empty;
        string eventID = string.Empty;

        DropDownList ddl = (DropDownList)sender;
        GridViewRow row2 = (GridViewRow)ddl.NamingContainer;
        giftID = ((Label)row2.FindControl("ID")).Text;  // grids index value
        eventID = ddl.SelectedValue;    // value of selected item in drop down
    }

    protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlPos = (DropDownList)e.Row.FindControl("ddlPos");
            ddlPos.DataSource = Session["Positions"]; // or from database
            ddlPos.DataTextField = "Position";
            ddlPos.DataValueField = "PosID";
            ddlPos.DataBind();
            ddlPos.Items.Insert(0, new ListItem("Select an item:", "0"));
        }
    }
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question