Solved

Gridview drop down list

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

19 Experts available now in Live!

Get 1:1 Help Now