0

我有一个带有分页的网格视图(页面大小 10),其中包含课程详细信息。当我将 gridview 数据保存到数据库时,它只保存 10 条记录(根据页面)。是否有任何获取所有gridview所有行的方法。例如,如果我的 gridview 包含 25 行分页,并且当我将数据保存到数据库中时,那么所有 25 行数据都插入到数据库中?请帮助...这是我的代码

          DataTable dt = new DataTable();
        dt.Columns.Add("row_index");
        dt.Columns.Add("edited_value");
        IList<int?> SeqNos = new List<int?>();


        foreach (GridViewRow gvr in gvViolationCodes.Rows)
        {
            TextBox tb = (TextBox)gvr.FindControl("txtSeqNo");
            Label lblSysid = (Label)gvr.FindControl("lblSysID");
            HiddenField hf = (HiddenField)gvr.FindControl("HiddenField1");
            if (tb.Text != hf.Value)
            {
                DataRow dr = dt.NewRow();
                SeqNos.Add(Convert.ToInt32(tb.Text));
                dr["row_index"] = lblSysid.Text;
                dr["edited_value"] = tb.Text;
                dt.Rows.Add(dr);
            }
        }
4

1 回答 1

0

您需要在 GrIdView 的 PageIndexChanging 事件中将数据保存在临时存储中。在最终保存时,将数据表发送到 Sql Server 过程或遍历数据表并将其保存在数据库中。按照步骤:

 protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        fnStoreGridData();
    }
    private void fnStoreGridData()
    {
        DataTable TempDataTable = new DataTable();
        if(Session["Data"]!=null){
            TempDataTable = Session["Data"] as DataTable;
        }
        else{
            TempDataTable.Columns.Add("exCourse", typeof(string)) ;
            TempDataTable.Columns.Add("exUniversityCourse", typeof(string)) ;
        }
        foreach (GridViewRow row in gvExportCourse.Rows)
        {
            Label exCourse = (Label)row.FindControl("gvlblCourseName");
            Label exUniversityCourse = (Label)row.FindControl("gvlblUniversityCourseName");
            if (exCourse != null)
            {
               if (!string.IsNullOrEmpty(exCourse.Text))
               {
                    TempDataTable.Rows.Add(exCourse.Text, exUniversityCourse.Text);
                    //TempDataTable is your datatable object. Make sure that datatable contains these columns
               }
            }
        }
    }

在最终保存中:

protected void button_Click(object s, EventArg e)
{
    fnStoreGridData(); //Here this will bind the data of current page
    DataTable TempDataTable = new DataTable();
    if(Session["Data"]!=null){
        TempDataTable = Session["Data"] as DataTable;
    }
    //Now loop through datatable and store the values
    foreach(DataRow in TempDataTable.Rows)
    {
        CourseInformation course = new CourseInformation();
        course.AddCourseMaster(dr["exCourse"].ToString(), dr["exUniversityCourse"].ToString());
    }
    label1.Text = "Course saved successfully.";
}
于 2013-09-13T06:27:08.920 回答