Solved

Gridview drop down list

Posted on 2008-06-19
2
1,808 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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