更新gridview行时未获取数据表中的值

时间:2016-03-25 06:44:08

标签: c# asp.net gridview datatable

我在gridview中有5行我正在尝试更新一行,在更新时,我写了下面的代码: -

protected void GrdProspective1_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
    if (Session["grdEdit"] != null)
    {
        DtCombo = (DataTable)Session["grdEdit"];
    }
    else
    {
        BindDatatable();
    }

    DataRow[] dataProsUpdate = DtCombo.Select("SR_NO = " + e.Record["SR_NO"]);

    dataProsUpdate[0]["PARTY_NAME"] = e.Record["PARTY_NAME"];
    dataProsUpdate[0]["FLAT_NO"] = e.Record["FLAT_NO"];
    dataProsUpdate[0]["LEASE_NUM"] = e.Record["LEASE_NUM"];
    dataProsUpdate[0]["ACTION"] = e.Record["ACTION"];
    dataProsUpdate[0]["NO_OF_DAYS"] = e.Record["NO_OF_DAYS"];
    dataProsUpdate[0]["REMARKS"] = e.Record["REMARKS"];

    GrdProspective1.DataSource = DtCombo;
    GrdProspective1.DataBind();
    AddToViewState("GrdProspective1");
}

但在此,我收到错误

  

索引超出了数组的范围。

当网格视图中有5行

时,我很困惑为什么datatable是emppty

我的数据表代码: -

public void BindDatatable()
{
    DataSet ds = new DataSet();
    DataRow dr;

    DataColumn SR_NO;
    DataColumn PARTY_NAME;
    DataColumn FLAT_NO;
    DataColumn LEASE_NUM;
    DataColumn ACTION;
    DataColumn NO_OF_DAYS;
    DataColumn REMARKS;

    DtCombo = new DataTable();

    SR_NO = new DataColumn("SR_NO", typeof(Int32));
    PARTY_NAME = new DataColumn("PARTY_NAME", typeof(String));
    FLAT_NO = new DataColumn("FLAT_NO", typeof(Int32));
    LEASE_NUM = new DataColumn("LEASE_NUM", typeof(Int32));
    ACTION = new DataColumn("ACTION", typeof(String));
    NO_OF_DAYS = new DataColumn("NO_OF_DAYS", typeof(Int32));
    REMARKS = new DataColumn("REMARKS", typeof(String));

    DtCombo.Columns.Add(SR_NO);
    DtCombo.Columns.Add(PARTY_NAME);
    DtCombo.Columns.Add(FLAT_NO);
    DtCombo.Columns.Add(LEASE_NUM);
    DtCombo.Columns.Add(ACTION);
    DtCombo.Columns.Add(NO_OF_DAYS);
    DtCombo.Columns.Add(REMARKS);
}

1 个答案:

答案 0 :(得分:1)

完整的解决方案:

protected void GrdProspective1_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
    if (Session["grdEdit"] != null)
    {
        DtCombo = (DataTable)Session["grdEdit"];
    }
    else
    {
        return;
    }

    DataRow[] dataProsUpdate = DtCombo.Select("SR_NO = " + e.Record["SR_NO"]);

    dataProsUpdate[0]["PARTY_NAME"] = e.Record["PARTY_NAME"];
    dataProsUpdate[0]["FLAT_NO"] = e.Record["FLAT_NO"];
    dataProsUpdate[0]["LEASE_NUM"] = e.Record["LEASE_NUM"];
    dataProsUpdate[0]["ACTION"] = e.Record["ACTION"];
    dataProsUpdate[0]["NO_OF_DAYS"] = e.Record["NO_OF_DAYS"];
    dataProsUpdate[0]["REMARKS"] = e.Record["REMARKS"];

    GrdProspective1.DataSource = DtCombo;
    GrdProspective1.DataBind();
    AddToViewState("GrdProspective1");
}

public void DisplayGridEdit() 
{ 
OracleCommand cmd = new OracleCommand("SELECT...", ObjPriCon); 

DtCombo = new DataTable(); 
OracleDataAdapter da = new OracleDataAdapter(cmd); 
da.Fill(DtCombo); 

GrdProspective1.DataSource = DtCombo; 
GrdProspective1.DataBind(); 
AddToViewState("GrdProspective1");
}