从数据库填充Gridview DropDownList

时间:2016-06-24 20:54:50

标签: c# asp.net gridview

我试图用数据库中的值填充Gridview。最初所有值都是null。但是,我希望能够将数据保存在数据库中。 (以上所有作品)。现在,当我继续'从我救的时候开始我无法获取数据来填充包含数据的单元格。

C#方法

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Session["usernameSess"] == null)
        {
            var returnUrl = Server.UrlEncode(Request.Url.PathAndQuery);
            Response.Redirect("~/Login.aspx?ReturnURL=" + returnUrl);
        }
        else
        {
            if (GlobalFunctions.GetADGroup(Session["usernameSess"].ToString(), "IT") == true
                || GlobalFunctions.GetADGroup(Session["usernameSess"].ToString(), "Accounting") == true)
            { }
            else
            {
                var returnUrl = Server.UrlEncode(Request.Url.PathAndQuery);
                Response.Redirect("/AccessDenied.aspx?ReturnURL=" + returnUrl);
            }

            if (Request.QueryString["emptyValue"] != null)
            {
                lblUser.Text = Request.QueryString["CreditCardUser"].ToString();
                lblError.Visible = true;
            }
        }

        string chName = GlobalFunctions.GetDisplayName(Session["usernameSess"].ToString());


        string email = GlobalFunctions.GetUserEmail(Session["usernameSess"].ToString());
        BindUserList(ddlUsers, email);
        BindGrid(sender, email);
    }
}


private void BindGrid(object sender, string email)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection(conStringTOA);
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand("GetCreditCardEntries");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@email", email);
    cmd.Parameters.AddWithValue("@chName", ddlUsers.SelectedValue);
    cmd.Connection = con;
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    gvCreditCards.DataSource = dt;
    gvCreditCards.DataBind();
}

ASP.NET GRIDVIEW

<asp:GridView ID="gvCreditCards" runat="server" Visible="true" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="ID" ItemStyle-CssClass="gvCCPadding" />
        <asp:BoundField DataField="fin_transaction_date" HeaderText="Transaction Date" ItemStyle-CssClass="gvCCPadding" ItemStyle-Width="50%" DataFormatString="{0:MM/dd/yyyy}" />
        <asp:BoundField DataField="acc_account_name" HeaderText="Account Name" ItemStyle-CssClass="gvCCPadding minWidth" />
        <asp:BoundField DataField="fin_charge" HeaderText="Charge" ItemStyle-CssClass="gvCCPadding" DataFormatString="{0:C}" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="fin_transaction_description" HeaderText="Vendor Name" ItemStyle-CssClass="gvCCPadding minWidth" />
        <asp:BoundField DataField="mch_state_province" HeaderText="State" ItemStyle-CssClass="gvCCPadding" />
        <asp:TemplateField HeaderText="Description" ControlStyle-Width="150px">
            <ItemTemplate>
                <asp:TextBox ID="txtUserDescription" runat="server" Text='<%# Eval("toa_user_description") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Receipt (Y/N)" ControlStyle-Width="75px">
            <ItemTemplate>
                <asp:DropDownList ID="ddlReceipt" runat="server">
                    <asp:ListItem Text="" Selected="True" Value=""></asp:ListItem>
                    <asp:ListItem Text="Yes" Selected="False" Value="Yes"></asp:ListItem>
                    <asp:ListItem Text="No" Selected="False" Value="No"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="GL Code" ControlStyle-Width="200px">
            <ItemTemplate>
                <asp:DropDownList ID="ddlGLCode" runat="server" DataSourceID="ds_AccountCode" DataValueField="toa_account_name" AppendDataBoundItems="true">
                    <asp:ListItem Selected="True" Text="" Value=""></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Department" ControlStyle-Width="200px">
            <ItemTemplate>
                <asp:DropDownList ID="ddlDepartment" runat="server" DataSourceID="ds_Department" DataValueField="toa_businessDepartment" AppendDataBoundItems="true">
                    <asp:ListItem Selected="True" Text="" Value=""></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Location" ControlStyle-Width="200px">
            <ItemTemplate>
                <asp:DropDownList ID="ddlLocation" runat="server" DataSourceID="ds_businessLocation" DataValueField="toa_site" AppendDataBoundItems="true">
                    <asp:ListItem Selected="True" Text="" Value=""></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="#e9e9e9" />
    <HeaderStyle BackColor="#e9e9e9" ForeColor="#D71826" />
    <EmptyDataTemplate>
        Empty Gridview
    </EmptyDataTemplate>
    <EmptyDataRowStyle ForeColor="#D71826" />
</asp:GridView>

0 个答案:

没有答案