0

我正在使用此处找到的 BulkEditGridView 控件(继承 GridView):http: //blogs.msdn.com/b/mattdotson/archive/2005/11/09/real-world-gridview-bulk-editing.aspx

现在我有一个保存按钮,当它被击中时应该只列出所有修改过的项目,但我得到的是“没有更新”。即使我更改了 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();
}

}

4

1 回答 1

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/

于 2012-09-17T17:54:42.817 回答