Solved

Gridview drop down list

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now