如何使用asp.net检查syncfusion中网格分组控件的rowstate

时间:2014-04-23 05:37:59

标签: c# syncfusion

我正在尝试将网格分组控件中的数据保存到数据库中。这是我在保存按钮单击事件中编写的代码。

foreach (TableRow row in GridGroupingControl1.TopLevelTable.Rows)
{
    if (row is GridRow)
    {
        GridRow gridRow = row as GridRow;

        if (gridRow.Record != null)
        {
            cmd = new SqlCommand("Insert into Employee(Name, Dept, Desg) values('" + gridRow.Record.GetValue("(Name") + "','" + gridRow.Record.GetValue("Dept") + "','" + gridRow.Record.GetValue("Desg") + "')", con);

            cmd.ExecuteNonQuery();
        }
     }
}

我第一次在网格中添加了2行并将这2行保存到了database.Next time我再次尝试在网格中添加2行,这时它保存了前2行和最后插入的行。所以我怎么样我可以找到rowstate,以便我新添加的最后两行必须保存在数据库中。

1 个答案:

答案 0 :(得分:0)

要使用新添加的记录更新数据库,在GridDataSourceControlRowAddingEventHandler中我们可以将添加的记录存储在ViewState中,然后在Save按钮单击事件中我们可以将ViewState中的记录插入到数据库中。

protected void Page_Load(object sender, EventArgs e)
    {
        ...
        this.GridGroupingControl1.DataSourceControlRowAdding += new GridDataSourceControlRowAddingEventHandler(GridGroupingControl1_DataSourceControlRowAdding);
    }

void GridGroupingControl1_DataSourceControlRowAdding(object sender,   GridDataSourceControlRowAddingEventArgs e)
    {
        List<Orders> records = new List<Orders>();
        if(ViewState["AddedRecord"] != null)
        {
            records = (List<Orders>)ViewState["AddedRecord"];
        }
        Orders obj = new Orders();
        obj.LastName = e.NewValues[0].ToString();
        obj.FirstName = e.NewValues[1].ToString();
        records.Add(obj);
        ViewState["AddedRecord"] = records;
        e.Cancel = true;
        e.Handled = true;
    }
private void SaveChanges()
    {
        if (ViewState["AddedRecord"] != null)
    {

        myConnection = new SqlCeConnection(ConnectionString);
        myConnection.Open();
        List<Orders> records1 = new List<Orders>();
        records1 = (List<Orders>)ViewState["AddedRecord"];
        foreach (var temp in records1)
        {
            SqlCeCommand command1 = new SqlCeCommand();
            command1.Connection = myConnection;
            command1.CommandText="INSERT INTO Employees([Last Name], [First Name]) VALUES(@LastName,@FirstName)";

            command1.Parameters.AddWithValue("@LastName", temp.LastName);
            command1.Parameters.AddWithValue("@FirstName", temp.FirstName);

            command1.ExecuteNonQuery();
        }
        ViewState["AddedRecord"] = null;
        myConnection.Close();
    }
    }