如何使用asp.net在syncfusion中检查网格分组控件的行状态

本文关键字:控件 状态 网格 检查 asp 何使用 net syncfusion | 更新日期: 2023-09-27 17:59:32

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

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行和最后插入的行。那么,我该如何找到行状态,以便我新添加的最后2行必须保存在数据库中。

如何使用asp.net在syncfusion中检查网格分组控件的行状态

要用新添加的记录更新数据库,在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();
    }
    }