Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
CSS
SQLiteDataAdapter adap;
SQLiteCommandBuilder cmdbl;
DataSet ds;
String Query;
DataTable dt;


Here is code for displaying data in Data-grid :
C#
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
C#
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
Posted
Updated 27-Nov-13 20:21pm
v2
Comments
AnthonyMG 28-Nov-13 2:21am    
Once you update your Grid , are you Refreshing the same .?

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

C#
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:
C#
yourDataGridView.Rows[RowIndex].Cells[CellIndex].Value = yourNewValue;
 
Share this answer
 
Comments
Member 8840306 28-Nov-13 3:49am    
dtgitems.Rows is not recognized in wpf ....!
Lauryx 28-Nov-13 4:04am    
So, are you using WinForms as you described in your Topic or WPF?
There is a major difference, since WPF has no build in DataGridView Control like WinForms has.
Hi! See this Article. It will help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900