c# drop-down-Liste in der grid-Ansicht

Ich bin eine grid-Ansicht, die ich habe, die kann ich Bearbeiten, aktualisieren, löschen " aus. Ich kann manuell fügen Sie ein Dropdown-Menü und den ausgewählten Wert Auffüllen, richtig, aber ich möchte in der Lage sein, die zum Auffüllen der drop-down-Datenbank ab und zeigen Sie den ausgewählten Wert korrekt), wenn es zu editieren.

Habe ich versucht eine Reihe von Suchanfragen/Optionen, aber kann nicht einen Weg finden, damit es funktioniert.

Mein code ist unten:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateData();
            lblMessage.Text = "";
        }

        string sSQL = "";

        //populate drop downs
        sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
            FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
            WHERE ErrorType = 'Category'
            ";
        //ORDER BY OrderBy, ErrorDescription";

        new DatabaseConnection().PopulateListBoxFromDB(sSQL, "", lstNewResolutionCategory);


        sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
            FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
            WHERE ErrorType = 'Severity'
            ";
        //ORDER BY OrderBy, ErrorDescription";

        new DatabaseConnection().PopulateListBoxFromDB(sSQL, "", lstNewResolutionSeverity);


    }//end page load


    protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
    {

        var ResolutionsID = GridView1.DataKeys[e.RowIndex].Value;

        GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;

        string sSQL = "Delete from BabelFish.dbo.Resolutions where ResolutionsID = @ResolutionsID";

        SqlCommand sCommand = new SqlCommand(sSQL);

        sCommand.Parameters.AddWithValue("@ResolutionsID", ResolutionsID);

        //run delete
        new DatabaseConnection().RSExecute(sCommand);

        lblMessage.Text = "Record deleted successfully !";

        GridView1.EditIndex = -1;

        this.PopulateData();

    }




    protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
    {

        var ResolutionsID = GridView1.DataKeys[e.RowIndex].Value;

        GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;

        TextBox txtResolutionDescription = row.FindControl("txtResolutionDescription") as TextBox;
        DropDownList drpErrorCategoryID = row.FindControl("ErrorCategory") as DropDownList;


        string sSQL = @"Update BabelFish.dbo.Resolutions set 
                    ResolutionDescription = @ResolutionDescription,
                    UserIDSolved = ISNULL(BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(@UserIDSolved), BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(REPLACE(@UserIDSolved, '-', ''))), 
                    DateTimeSolved = ISNULL(DateTimeSolved, GetDate())                        
                    where ResolutionsID = @ResolutionsID";

        SqlCommand sCommand = new SqlCommand(sSQL);

        sCommand.Parameters.AddWithValue("@ResolutionDescription", txtResolutionDescription.Text.Trim());
        sCommand.Parameters.AddWithValue("@ResolutionsID", ResolutionsID);
        sCommand.Parameters.AddWithValue("@UserIDSolved", UserID);

        //run update
        new DatabaseConnection().RSExecute(sCommand);


        lblMessage.Text = "Record updated successfully !";

        GridView1.EditIndex = -1;

        this.PopulateData();

    }




    private void PopulateData()
    {
        string sSQL = @"SELECT
                ResolutionsID, ErrorTableID, BabelFish.dbo.fn_GetUserNameFromTeamMemberTable(UserIDSolved) as UserIDSolved, 
                DateTimeSolved, ResolutionDescription, ResolutionCategory, ResolutionSeverity, IsActive
                FROM BabelFish.dbo.Resolutions (NOLOCK)
                Where ErrorTableID = '" + ErrorTableID + "'";

        DataTable dt = DatabaseAccessing.DatabaseConnection.GetDataTable(sSQL);

        //only do if more then 1 row exists
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;

            GridView1.DataBind();
        }
        else
        {
            lblMessage.Text = "No Rows Exist.";
        }




    }


    protected void AddRow(object sender, EventArgs e)
    {
        //get values to add to database
        string txtResolutionDescription = txtNewResolutionDescription.Text.ToString();

        string lstCategoryID = lstNewResolutionCategory.SelectedValue;
        string lstSeverityID = lstNewResolutionSeverity.SelectedValue;

        string sSQL = @"INSERT INTO BabelFish.dbo.Resolutions (
            ErrorTableID, UserIDSolved, DateTimeSolved, ResolutionDescription, 
            ResolutionCategory, ResolutionSeverity, IsActive
        )

        VALUES (
            @ErrorTableID, ISNULL(BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(@UserIDSolved), BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(REPLACE(@UserIDSolved, '-', ''))), 
            GetDate(), @ResolutionDescription,
            @ResolutionCategory, @ResolutionSeverity, 1
        )";


        SqlCommand sCommand = new SqlCommand(sSQL);

        sCommand.Parameters.AddWithValue("@ErrorTableID", ErrorTableID);
        sCommand.Parameters.AddWithValue("@UserIDSolved", UserID);
        sCommand.Parameters.AddWithValue("@ResolutionDescription", txtResolutionDescription);
        sCommand.Parameters.AddWithValue("@ResolutionCategory", lstCategoryID);
        sCommand.Parameters.AddWithValue("@ResolutionSeverity", lstSeverityID);


        //run update
        new DatabaseConnection().RSExecute(sCommand);


        lblMessage.Text = "Record successfully added!";

        GridView1.EditIndex = -1;

        this.PopulateData();
    }


    protected void EditRow(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;

        this.PopulateData();

        /*
        //not working, ddl is NULL
        var ddl = (DropDownList)GridView1.FindControl("selResolutionSeverity");

        string sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
            FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
            WHERE ErrorType = 'Severity' ";


        DataSet DS = new DatabaseAccessing.DatabaseConnection().DS(sSQL);

        ddl.DataSource = DS;
        ddl.DataTextField = "DisplayText";
        ddl.DataValueField = "Value";
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("-- Select --", "0"));
         * */
    }



    protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        this.PopulateData();
    }


    protected void ChangePage(object sender, GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;
        this.PopulateData();

    }



    <asp:Label ID="lblMessage" runat="server" ForeColor="Green" EnableViewState="false" />

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="false" 
        Width="100%" 
        OnRowEditing="EditRow" 
        OnRowCancelingEdit="CancelEditRow"
        OnRowUpdating="UpdateRow" 
        DataKeyNames="ResolutionsID" 
        OnRowDeleting="DeleteRow" 
        AllowPaging="true"
        PageSize="50" 
        OnPageIndexChanging="ChangePage"            

        >

    <Columns>

        <asp:TemplateField HeaderText="Edit">

            <ItemTemplate>
                <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" />
            </ItemTemplate>

            <EditItemTemplate>

                <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" />

                <asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />

            </EditItemTemplate>

        </asp:TemplateField>


        <asp:BoundField HeaderText="ResolutionsID" DataField="ResolutionsID" ReadOnly="true" />

        <asp:TemplateField HeaderText="ResolutionDescription">
            <ItemTemplate><%# Eval("ResolutionDescription")%></ItemTemplate>

            <EditItemTemplate>
                <asp:TextBox ID="txtResolutionDescription" runat="server" Text='<%# Eval("ResolutionDescription") %>'/>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:BoundField HeaderText="UserIDSolved" DataField="UserIDSolved" ReadOnly="true" />

        <asp:TemplateField HeaderText="Category">
            <ItemTemplate>
                <%# Eval("ResolutionCategory")%>
            </ItemTemplate>

            <HeaderStyle HorizontalAlign="Left" />
                <EditItemTemplate>  
                    <asp:DropDownList ID="selResolutionCategory" runat="server" SelectedValue='<%# Eval("ResolutionCategory") %>'>         
                        <asp:ListItem Text="-- Select One --" Value="0" />
                        <asp:ListItem Text="cat1" Value="1" />
                        <asp:ListItem Text="cat2" Value="2" />        
                    </asp:DropDownList>
                </EditItemTemplate>
        </asp:TemplateField>


        <asp:TemplateField HeaderText="Severity">
            <ItemTemplate>
                <%# Eval("ResolutionSeverity")%>
            </ItemTemplate>

            <HeaderStyle HorizontalAlign="Left" />
                <EditItemTemplate>  
                    <asp:DropDownList ID="selResolutionSeverity" runat="server">                                     
                    </asp:DropDownList>
                </EditItemTemplate>
        </asp:TemplateField>


        <asp:TemplateField HeaderText="Delete?">
            <ItemTemplate>
                <span onclick="return confirm('Are you sure to delete?')">
                    <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete" />
                </span>
            </ItemTemplate>

        </asp:TemplateField> 


    </Columns>

    <AlternatingRowStyle BackColor="White" />

    <EditRowStyle BackColor="#efefef" />

    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

    <RowStyle BackColor="#EFF3FB" />

    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

    <SortedAscendingCellStyle BackColor="#F5F7FB" />

    <SortedAscendingHeaderStyle BackColor="#6D95E1" />

    <SortedDescendingCellStyle BackColor="#E9EBEF" />

    <SortedDescendingHeaderStyle BackColor="#4870BE" />

    </asp:GridView>
InformationsquelleAutor Brad | 2014-01-06
Schreibe einen Kommentar