获取新添加的行值,以便在网格控制devexpress winform中插入

时间:2016-05-10 15:11:43

标签: winforms devexpress

我实际上是想从我的网格控件(devexpress)中获取新添加的行的新值,而我设置了#34; AllowAddRows"为真; 我可以像这样得到更新的

  

Object row = ListeQual.GetRow(ListeQual.FocusedRowHandle);

但我可以在插入

的add事件中获取新值

2 个答案:

答案 0 :(得分:0)

您可以创建一个按钮,以便添加新行:

  private void AddNewRow_Click(object sender, EventArgs e)
        {
           // (ListeQual.MainView as DevExpress.XtraGrid.Views.Grid.GridView).AddNewRow();
             ListeQual.AddNewRow(); 
        }

创建一个方法如何将新行作为参数传递给datatbase中保存:

private int insertData(DataRow dr)
    {
        string connectionString = "your connection string here";
        int nb = 0;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO Data (Col1, Col2, Col3) VALUES (@col1, @col2, @col3)");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@col1", dr[0]);
            cmd.Parameters.AddWithValue("@col2", dr[1]);
            cmd.Parameters.AddWithValue("@col3", dr[2]);
            connection.Open();
           nb= cmd.ExecuteNonQuery();
        }
        return nb;
    }

最后,您可以处理事件ValidateRow

 private void ListeQual_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = ListeQual as GridView;
            if (view.IsNewItemRow(e.RowHandle))
            {
              DataRow dw = view.GetDataRow(e.RowHandle);
              insertData(dw);
            }

        }

答案 1 :(得分:0)

试试这个:

private void ListeQual_ValidateRow(object sender, ValidateRowEventArgs e)
{
    GridView view = sender as GridView;
    if (view.IsNewItemRow(e.RowHandle))
    {
        DataRow row = view.GetDataRow(e.RowHandle);
    }
}