0

我有两个下拉列表,用于生成将被编辑和更新的网格视图。我不知道为什么 gridview 不会填充。我附上了我拥有的编码。我已经尝试过隐藏字段,我现在有似乎正确填充的参数,我对为什么看不到 gridview 感到非常困惑。作为警告,我需要能够通过文本框或下拉列表更新行中的某些列。如果我更改对 gridview 的数据源的查询以引入数据库中的每一行,我可以看到所有内容并完美地编辑和更新。

这是代码,首先是我的 aspx,然后是 aspx.cs

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Annos3.aspx.cs" Inherits="SHCAnnotation.WebForm2" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="dlJobName" runat="server" AutoPostBack="True" 
            DataSourceID="JobName" DataTextField="Name" DataValueField="Name" 

            style="z-index: 1; left: 25px; top: 50px; position: absolute; bottom: 257px; height: 45px; width: 242px;" 
            onselectedindexchanged="dlJobName_SelectedIndexChanged" >
        </asp:DropDownList>
        <asp:SqlDataSource ID="JobName" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SRM_MetricConnectionString %>" 
            SelectCommand="SELECT DISTINCT Name  FROM vw_GridviewSource WHERE (DueDt &gt; sysdatetime()) ORDER BY Name">
        </asp:SqlDataSource>

    <asp:GridView ID="gvSummary" runat="server" AutoGenerateColumns = "False" AllowSorting = "true" 
         OnRowCancelingEdit = "CancelEdit" OnRowEditing = "EditSummary" OnRowDataBound = "RowDataBound"
        OnRowUpdating = "UpdateSummary" CellPadding="4" ForeColor="#333333" 
        GridLines="None" DataKeyNames="AnnotationNumber, Cust, Name, AnnotationDate"
        style="z-index: 1; left: 10px; top: 130px; position: absolute; height: 133px; width: 257px" >
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <columns>
    <asp:CommandField ShowEditButton="true" />
        <asp:TemplateField HeaderText = "Annotation Number">
        <ItemTemplate>
            <asp:Label ID = "lblAnno" runat="server" Text='<%# Eval("AnnotationNumber") %>'> </asp:Label>
             </ItemTemplate>
            <HeaderStyle Width="300px" />
            <ItemStyle Width="300px" HorizontalAlign="Center" />
           </asp:TemplateField>
                <asp:TemplateField HeaderText = "Annotation By" HeaderStyle-Width = "250">
        <ItemTemplate>
            <asp:Label ID = "lblAnnoBy" runat="server" Text='<%# Eval("AnnotationBy") %>' Width = "150"> </asp:Label>
             </ItemTemplate>
            <HeaderStyle Width="450" />
            <ItemStyle Width="450" HorizontalAlign="Center" />

           </asp:TemplateField>
                <asp:TemplateField HeaderText = "Annotation Type">
        <ItemTemplate>
            <asp:Label ID = "lblAnnoType" runat="server" Text='<%# Eval("AnnotationType") %>' Width = "150"> </asp:Label>
             </ItemTemplate>
            <HeaderStyle Width="450px" />
            <ItemStyle Width="450px" HorizontalAlign="Center" />
           </asp:TemplateField>
        <asp:TemplateField HeaderText = "Business Unit" HeaderStyle-Width = "250">
        <ItemTemplate>
            <asp:Label ID = "lblBU" runat="server" Text='<%# Eval("Unit") %>' Width = "250" ></asp:Label>
            </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblBU" runat="server" Text='<%# Eval("Unit") %>' Visible ="false"></asp:Label>
            <asp:DropDownList ID = "dlBU" runat="server" AutoPostBack = "true">
            </asp:DropDownList>
            </EditItemTemplate>
            <HeaderStyle Width="300px" HorizontalAlign="Center" />
            <ItemStyle Width="300px" HorizontalAlign="Center" />
            </asp:TemplateField>
        <asp:TemplateField HeaderText = "Error Type" HeaderStyle-Width = "250">
         <ItemTemplate>
            <asp:Label ID = "lblET" runat="server" Text='<%# Eval("ErrorType") %>' Width = "250" ></asp:Label>
            </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblET" runat="server" Text='<%# Eval("ErrorType") %>' Visible ="false"></asp:Label>
            <asp:DropDownList ID = "dlET" runat="server" AutoPostBack = "true">
            </asp:DropDownList>
            </EditItemTemplate>
            <HeaderStyle Width="300px" HorizontalAlign="Center" />
            <ItemStyle Width="300px" HorizontalAlign="Center" />
            </asp:TemplateField>    
       <asp:TemplateField HeaderText = "Actual Agency Error" HeaderStyle-Width = "250">
         <ItemTemplate>
            <asp:Label ID = "lblAA" runat="server" Text='<%# Eval("ActualAgencyError") %>' Width = "250" ></asp:Label>
            </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblAA" runat="server" Text='<%# Eval("ActualAgencyError") %>' Visible ="false"></asp:Label>
            <asp:DropDownList ID = "dlAA" runat="server" AutoPostBack = "true">
            </asp:DropDownList>
            </EditItemTemplate>
            <HeaderStyle Width="300px" HorizontalAlign="Center" />
            <ItemStyle Width="300px" HorizontalAlign="Center" />
            </asp:TemplateField>      
        <asp:TemplateField HeaderText = "AnnotationComments">
        <ItemTemplate>
            <asp:Label ID = "lblAnnoComm" runat ="server" Text = '<%# Eval("AnnotationComments") %>' Width = "400"></asp:Label>  
             </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText = "sgk Comments">
        <ItemTemplate>
            <asp:Label ID = "lblsgkComm" runat ="server" Text = '<%# Eval("sgkComments") %>' Width = "400"></asp:Label>  
             </ItemTemplate>
        <EditItemTemplate>
            <asp:Textbox ID="tbsgkComm" runat="server" Text='<%# Eval("sgkComments") %>' TextMode = "MultiLine" Columns = "40"  ></asp:Textbox>   
        </EditItemTemplate>
            <HeaderStyle HorizontalAlign="Center" Width="400px" />
            <ItemStyle HorizontalAlign="Justify" Width="400px" />
           </asp:TemplateField>


    </columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

    </div>
    <asp:DropDownList ID="dlStage" runat="server" AutoPostBack="True" 
        DataSourceID="Stage" DataTextField="Annotation_Date" 
        DataValueField="Annotation_Date" Height="45px" 
        style="z-index: 1; left: 360px; top: 50px; position: absolute" 
        Width="240px" onselectedindexchanged="dlStage_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Stage" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SRM_MetricConnectionString %>" 
        SelectCommand="SELECT DISTINCT [AnnotationDate] AS Annotation_Date FROM [vw_GridviewSource] WHERE ([Name] = @Name)">
        <SelectParameters>
            <asp:ControlParameter ControlID="dlJobName" Name="Name" 
                PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:HiddenField ID = "hfJN" runat="server" />
    <asp:HiddenField ID = "hfST" runat="server" />
    </form>
</body>
</html>

aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;

namespace SHCAnnotation
{
    public partial class WebForm2 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //hfJN.Value = dlJobName.SelectedIndex.ToString();
                //hfST.Value = dlStage.SelectedIndex.ToString();
                //this.BindDataInit();


            }
        }

        protected void EditSummary(object sender, GridViewEditEventArgs e)
        {            

            gvSummary.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvSummary.EditIndex = -1;
            BindData();
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && gvSummary.EditIndex == e.Row.RowIndex)
            {

                DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU");

                string Cust = gvSummary.DataKeys[e.Row.RowIndex].Values["Cust"].ToString();
                string BUquery = "select distinct Unit from vw_BU where Business ='" + Cust + "'";
                SqlCommand BUcmd = new SqlCommand(BUquery);
                dlBU.DataSource = GetData(BUcmd);
                dlBU.DataTextField = "Unit";
                dlBU.DataValueField = "Unit";
                dlBU.DataBind();
                dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true;


                DropDownList dlET = (DropDownList)e.Row.FindControl("dlET");

                string ETquery = "select distinct ErrorType from ErrorType";
                SqlCommand ETcmd = new SqlCommand(ETquery);
                dlET.DataSource = GetData(ETcmd);
                dlET.DataTextField = "ErrorType";
                dlET.DataValueField = "ErrorType";
                dlET.DataBind();
                dlET.Items.FindByValue((e.Row.FindControl("lblET") as Label).Text).Selected = true;

                DropDownList dlAA = (DropDownList)e.Row.FindControl("dlAA");

                string AAquery = "select distinct AAA from ActualAgencyError";
                SqlCommand AAcmd = new SqlCommand(AAquery);
                dlAA.DataSource = GetData(AAcmd);
                dlAA.DataTextField = "AAA";
                dlAA.DataValueField = "AAA";
                dlAA.DataBind();
                dlAA.Items.FindByValue((e.Row.FindControl("lblAA") as Label).Text).Selected = true;

            }
        }

        protected void UpdateSummary(object sender, GridViewUpdateEventArgs e)
        {

            string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value;
            string ET = (gvSummary.Rows[e.RowIndex].FindControl("dlET") as DropDownList).SelectedItem.Value;
            string AA = (gvSummary.Rows[e.RowIndex].FindControl("dlAA") as DropDownList).SelectedItem.Value;
            string AnnotationNumber = gvSummary.DataKeys[e.RowIndex].Value.ToString();
            string sgkComments = (gvSummary.Rows[e.RowIndex].FindControl("tbsgkComm") as TextBox).Text;
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                string query = "update vw_GridviewSource set [BusinessUnit] = @BU, [ErrorType] = @ET, [sgkComments] = @sgk, [ActualAgencyError] = @AA where [AnnotationNumber] = @AnnoNum";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@BU", BU);
                    cmd.Parameters.AddWithValue("@AnnoNum", AnnotationNumber);
                    cmd.Parameters.AddWithValue("@ET", ET);
                    cmd.Parameters.AddWithValue("@AA", AA);
                    cmd.Parameters.AddWithValue("@sgk", sgkComments);



                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
        }

        private void BindDataInit()
        {



            SqlCommand cmd = new SqlCommand();
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            cmd.Connection = con;
            cmd.CommandText = "select [AnnotationNumber],[AnnotationBy],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust,Name,AnnotationDate from vw_GridviewSource" ;

            gvSummary.DataSource = GetData(cmd);
            gvSummary.DataBind();
        }




        private void BindData()
        {
            string selectedName = dlJobName.SelectedValue;
            string selectedStage = dlStage.SelectedValue;


            //SqlCommand cmd = new SqlCommand(query);
            SqlCommand cmd = new SqlCommand();
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            cmd.Connection = con;
            cmd.CommandText = "select [AnnotationNumber],[AnnotationBy],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust,Name,AnnotationDate from vw_GridviewSource where Name = @p1 and AnnotationDate = @p2";
            //cmd.Parameters.AddWithValue("@p1",selectedName);
            //cmd.Parameters.AddWithValue("@p2",selectedStage);

            gvSummary.DataSource = GetData(cmd);
            gvSummary.DataBind();
        }

        private DataTable GetData(SqlCommand cmd)
        {
            string selectedName = dlJobName.SelectedValue;
            string selectedStage = dlStage.SelectedValue;

            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@p1",selectedName);
                    cmd.Parameters.AddWithValue("@p2",selectedStage);
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        protected void dlJobName_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string JobName = Application["gJob"].ToString();
            BindData();
        }

        protected void dlStage_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string stage = Application["gStage"].ToString();
            BindData();
        }



    }
}
4

0 回答 0