0

尝试在 C# 应用程序中创建数据网格,当您单击编辑按钮时,整个网格消失。我让这个工作了片刻,但无法恢复。任何人都可以在我的代码中看到任何错误吗?

这是 ASPX:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="frmViewLoadHistory.aspx.cs" Inherits="PBR.ViewLoadHistory" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <br />
    <div id="divGrid" style='position:absolute; width:920px; height:400px; overflow:auto'>
        <asp:DataGrid ID="DataGrid_Roster" runat="server"
    AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None"
    OnCancelCommand="DataGrid_Roster_CancelCommand" 
    OnUpdateCommand="DataGrid_Roster_UpdateCommand" 
    OnEditCommand="DataGrid_Roster_EditCommand">
            <AlternatingItemStyle Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <EditItemStyle BackColor="#999999" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <PagerStyle BackColor="#284775" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <Columns>
            <asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" 
                EditText="Edit" UpdateText="Update"></asp:EditCommandColumn>
        </Columns>

        </asp:DataGrid>

    </div> 
    <br />

</asp:Content>

和代码隐藏:

public string stringSelectedValue { get; set; }
string str2 = System.Configuration.ConfigurationManager.ConnectionStrings["RosterConnectionString"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {
        Show_Data();
    }
}

public void Show_Data()
{
    SqlConnection sqlconnectionStatus2 = new SqlConnection(str2);
    string sqlquery2;

    sqlquery2 = "SELECT * FROM [tblcensus]";

    SqlConnection con2 = new SqlConnection(str2);
    SqlCommand cmd2 = new SqlCommand(sqlquery2, con2);
    SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);

    // Fill the DataSet.
    DataSet ds2 = new DataSet();
    adapter2.Fill(ds2, "dailyview");

    // Perform the binding.
    DataGrid_Roster.DataSource = ds2;
    DataGrid_Roster.DataBind();

}

protected void DataGrid_Roster_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    DataGrid_Roster.EditItemIndex = e.Item.ItemIndex;
    DataGrid_Roster.DataBind();
}

protected void DataGrid_Roster_UpdateCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    // The quantity is in the 7th column.
    TableCell quantityCell = e.Item.Cells[6];

    // The TextBox is the 0th element of the Controls collection.
    TextBox quantityBox = (TextBox)quantityCell.Controls[0];

    // Extract the quantity from the box.
    int quantity = System.Int32.Parse(quantityBox.Text);

    // Use quantity to update the data source.

    // Switch out of edit mode.
    DataGrid_Roster.EditItemIndex = -1;
    DataGrid_Roster.DataBind();
}

protected void DataGrid_Roster_CancelCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    DataGrid_Roster.EditItemIndex = -1;
    DataGrid_Roster.DataBind();
}

我可以发誓,当我让它工作时,我不必担心 IsPostBack 部分,但是在抛出错误之前它似乎没有处理任何事情。现在我没有收到任何错误,但 DataGrid 根本没有出现。

4

1 回答 1

2

在您的 EditCommand 事件处理程序中,调用您的 Show_Data 方法,而不是仅在网格上调用 Databind。

DataGrid_Roster.EditItemIndex = e.Item.ItemIndex;
Show_Data();

数据不会在回发之间缓存,因此每次都需要获取它,或者您需要设置其他缓存机制。

于 2013-07-02T16:10:57.467 回答