Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gridview drop down list

Posted on 2008-06-19
2
Medium Priority
?
1,822 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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