修改GridView后,BulkEditGridView DirtyRows计数为0

时间:2012-09-17 16:46:18

标签: c# asp.net gridview

我使用的是BulkEditGridView控件(它继承了GridView):http://blogs.msdn.com/b/mattdotson/archive/2005/11/09/real-world-gridview-bulk-editing.aspx

现在我有一个保存按钮,它应该只是在命中时列出所有修改过的项目,但我得到的是#34;没有更新。"即使我更改了DataGrid中的文本。为什么没有DirtyRows正确填充?我在更新数据库之前测试了这个。

我的aspx页面如下:

<rwg:BulkEditGridView ID="EditableGrid" AutoGenerateColumns="False" OnRowUpdating="EditableGrid_RowUpdating" OnRowEditing="EditRecord" SaveButtonID="SaveButton" runat="server">
            <Columns>
                <asp:BoundField HeaderText="Question" DataField="Question" />
                <asp:BoundField HeaderText="A" DataField="a" />
                <asp:BoundField HeaderText="B" DataField="b" />
                <asp:BoundField HeaderText="C" DataField="c" />
                <asp:BoundField HeaderText="D" DataField="d" />
                <asp:BoundField HeaderText="E" DataField="e" />
                <asp:BoundField HeaderText="F" DataField="f" />
                <asp:BoundField HeaderText="Correct Answer" DataField="CorrectAns" />
                <asp:CheckBoxField HeaderText="Shuffle Answers" DataField="ShuffleAnswers" />
                <asp:BoundField HeaderText="Course Objective" DataField="CourseObjective" />                    
            </Columns>
        </rwg:BulkEditGridView>

我的代码背后是这样的:

public partial class editExam : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string courseCode = "";
    courseCode = Request.QueryString["code"];

    if (courseCode.Length > 0)
    {
        //Show all of the course questions.
        Connection conn = new Connection();
        SqlParameter p = new SqlParameter("@CourseCode", SqlDbType.VarChar, 10);
        p.Value = Connection.validateString(courseCode);
        conn.close();
        //Gets the exam to display from the database
        DataTable dt = conn.query("getExam", p);
        if (dt.Rows.Count == 0)
        {
            //Display an error message
            displayError();
        }
        else
        {
            //Show all courses
            buildPage(dt);
        }
    }
    else
    {
        //Display an error message
        displayError();
    }
}

private void buildPage(DataTable dt)
{
    EditableGrid.DataSource = dt;
    EditableGrid.DataBind();
}
protected void SaveButton_Click(object sender, EventArgs e)
{
    if (EditableGrid.DirtyRows.Count == 0)
    {
        pageContent.InnerHtml = "No updates have been made.";
    }
}

protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    StringBuilder rowIndexes = new StringBuilder();
    foreach (GridViewRow row in EditableGrid.DirtyRows)
    {
        rowIndexes.Append((row.RowIndex + 1) + ", ");
    }

    pageContent.InnerHtml = "updates made to: " + rowIndexes.ToString();
}

}

1 个答案:

答案 0 :(得分:0)

我忘了创建一个EditRecord方法。应该看起来像这样:

protected void EditRecord(object sender, GridViewEditEventArgs e)
{
    EditableGrid.EditIndex = e.NewEditIndex;
    buildPage();
}

我也遵循了这篇文章:http://www.dotnetfunda.com/articles/article29.aspx,它解释了如何正确使用GridView控件。

这也很有用:对于RowUpdating方法:http://dotnetdiscussion.wordpress.com/2008/04/06/aspnet-how-to-use-bulkeditgridview-to-save-hours-in-database-editing/