网格视图编辑并保存到数据库Winform

时间:2013-12-18 23:46:17

标签: c# winforms gridview devexpress

对于Windows应用程序,我有一个带有gridview的表单。在表单中,我有一个编辑按钮。如果用户选择一行并单击“编辑”,则必须使用所选数据重定向到另一个表单。然后,用户可以输入详细信息。当用户单击“保存数据”时,它应该保存并返回数据库。

FIRSTListForm

private void btnNewFIRSTList_ItemClick(object sender, ItemClickEventArgs e)`
{
    FIRSTEditForm FIRSTEditForm = new FIRSTEditForm(FIRSTID);`

    FIRSTEditForm.Show();

}

private void btnEditFIRSTList_ItemClick(object sender, ItemClickEventArgs e)
{
    //I need to know which row I selected before to Edit button

    //object IdFirst = ((GridView)sender).GetRowCellValue(e.RowHandle, "IDFIRST");

    //I need like a GetRowCellValue

    FIRSTEditForm.Show();
}

3 个答案:

答案 0 :(得分:0)

您需要一种保存方法将数据写回数据库并在保存按钮上单击

进行调用

Here is multiple examples of exactly what your wanting

如果您想打开单独的表单,则需要This

之类的内容

答案 1 :(得分:0)

如果您使用带有winforms的数据集,可以尝试:
1)检查您的gridview是否有任何选定的行

if (gridView1.SelectedRowsCount > 0)
   {
         ......
   }

2)如果是这种情况,则使用强名称的DataRow类和BindingSource.Current

 YoutDataSet.YourTableOrResultSetRow Row;
 var P = YourTableOrResultSetBindingSource.Current as DataRowView;
 Row = (P.Row as YoutDataSet.YourTableOrResultSetRow );

 int id = Row.ColumnNameThatContainstheID; //this will give you the ID you're after

答案 2 :(得分:0)

先试试,

private void gridView_DoubleClick(object sender, EventArgs e)
{
        if (gridView.GetFocusedRow() == null) return;

        if (gridView.GetFocusedRowCellValue("ID") == null) return;

        string id = gridView.GetFocusedRowCellValue("ID").ToString();

        FIRSTEditForm frm = new FIRSTEditForm(id);
        frm.Show();
}

FIRSTEditForm中使用获取ID并绑定所有控件并保存到数据库。

我使用gridView_DoubleClick代替btnEditFIRSTList_ItemClick

希望它能解决!