如何使用C#插入和更新datagrid行 [英] How I can Insert and Update datagrid row in using C#

查看:85
本文介绍了如何使用C#插入和更新datagrid行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发C#而不使用任何framework.until现在我成功地在Datagrid上执行了以下任务。

1.通过绑定数据库表来显示数据网格中的数据

2.根据主键从数据网格中获取所选行数据

这是我的变量声明:

  SQLiteDataAdapter  adap; 
SQLiteCommandBuilder cmdbl;
DataSet ds;
字符串查询;
DataTable dt;





这是显示数据的代码在数据网格中:

 尝试 
{
查询= 从项目中选择* ;
adap = new SQLiteDataAdapter(Query,GlobalVars.conn);
ds = new DataSet();
adap.Fill(ds, Items);
dt = ds.Tables [ 0 ];
dtgitems.DataContext = ds.Tables [ 0 ]。DefaultView;
dtgitems.ItemsSource = dt.DefaultView;
ds.Dispose();
adap.Dispose();
dt.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}



工作正常

这是我更新数据网格的代码

 尝试 
{
cmdbl = new SQLiteCommandBuilder(ADAP);
adapt.Update(ds, Items);
// ds.Tables [0] .Clear();
}
catch (例外情况)
{

}



它不工作。有谁知道我如何在数据网格中完成这个更新功能? 。请帮我纠正这段代码更新opreation。谢谢

解决方案

为了插入数据网格我使用以下逻辑:



 yourDataGridView.Rows.Clear(); 

SqlCommand cmd = new SqlCommand(YourSqlQuery,dbConnection);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
yourDataGridView.Rows.Add(dr [ 0 ]。ToString(),( bool )dr [ 1 ], ( int )dr [ 2 ]);
}
dr.Close();





如果你正在处理不同的线程,收集数据,您可以将SqlDataReader中的数据添加到DataTable中并将其传递给UI线程以填充DataGrid。







要更新DataGrid的单个值,您可以使用以下内容:

 yourDataGridView.Rows [RowIndex] .Cells [CellIndex] .Value = yourNewValue; 


嗨!请参阅本文。它会对你有帮助。


I am developing C# without using any framework.until now i successfully performed following task on Datagrid.
1.Displaying data in Data-grid by binding with database table
2.Getting the selected row data from Data-grid based on primary key
Here is my variable declaration :

SQLiteDataAdapter adap;
SQLiteCommandBuilder cmdbl;
DataSet ds;
String Query;
DataTable dt;



Here is code for displaying data in Data-grid :

try
{
    Query = "Select * from Items";
    adap = new SQLiteDataAdapter(Query, GlobalVars.conn);
    ds = new DataSet();
    adap.Fill(ds, "Items");
    dt = ds.Tables[0];
    dtgitems.DataContext = ds.Tables[0].DefaultView;
    dtgitems.ItemsSource = dt.DefaultView;
    ds.Dispose();
    adap.Dispose();
    dt.Dispose();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}


It is working fine
and here is my code for updating that data-grid

try
 {
     cmdbl = new SQLiteCommandBuilder(adap);
     adap.Update(ds, "Items");
    // ds.Tables[0].Clear();
 }
 catch (Exception ex)
 {

 }


it is not working .Does anyone knows how i can accomplish this update functionality in data-grid ? .Please help me to correct this code for update opreation .Thanks

解决方案

For inserting in a datagrid I'm using the following logic:

yourDataGridView.Rows.Clear();

SqlCommand cmd = new SqlCommand(YourSqlQuery, dbConnection);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
     yourDataGridView.Rows.Add(dr[0].ToString(), (bool)dr[1], (int)dr[2]);
}
dr.Close();



If you're working with differen threads, collecting the data, you could add the data from the SqlDataReader into a DataTable and pass it to your UI Thread to fill the DataGrid.



For updating single values of your DataGrid you could use something like this:

yourDataGridView.Rows[RowIndex].Cells[CellIndex].Value = yourNewValue;


Hi! See this Article. It will help you.


这篇关于如何使用C#插入和更新datagrid行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆