Hi
I have a problem with refreshing the display in DataGridView. I bounded that control to a Data Source, and when I start the application, everything works just fine. I also made a section where I add new data to that dataset (by using ExecuteCommand method), and that works fine as well (I know this because when I close and start my application the new data appears normaly). The problem appears when I add new data to my database, the data doesent show in datagrid view. I found lots of instructions about this problem, but none of them seem to work. I tried the dataGridView.Refresh() method - I can see that the control is redrawn, but the displayed data stays the same, and I also tried the Invalidate() and Update method, both with same result. I even tried to set the DataSource propertie to null, and then refill it, but that only gives me an empty table with only one row.
Please advise
Tnx
Loading
Sam HobbsPosted Nov 22, 2011, 4:58 AM
The myDataSet.myDataTable class has a member named Rows. You can add rows to that collection. There is probably a class myDataSet.myTableRow. You can create instances of myDataSet.myTableRow and fill it with data then add that to the rows collection. When you have added all the data you need to add, then call myTableAdapter.Update(myDataTable), or something like that; I do not know the name of your TableAdapter. Note that myTableAdapter would be the name of the instance of the TableAdapter, not the class. If you get that accomplished, then the DataGridView will get updated when you call the Update.
I know that TableAdapters take time to learn, but when you have learned how to use them then your code will be smaller and easier in the future.
Sam HobbsPosted Nov 22, 2011, 6:00 AM
I know it can be frustrating figuring out how to use TableAdapters.
Neven DraskovicPosted Nov 22, 2011, 5:08 AM
Neven DraskovicPosted Nov 22, 2011, 3:32 AM
PDC.ExecuteCommand("INSERT INTO MyTable VALUES ('" + Convert.ToString(DateTime.Now) + "','" + Convert.ToString(this.comboBox1.SelectedItem) + "-" + Convert.ToString(this.comboBox2.SelectedItem) + "','" + Convert.ToString(this.comboBox3.SelectedItem) + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "')");
Sam HobbsPosted Nov 22, 2011, 3:27 AM
Neven DraskovicPosted Nov 22, 2011, 3:03 AM
I managed to get it working by declaring a DataSet variable, and whenever I insert a new entry into the database I fill that variable with the content's of the database, and set the dataGridView's to pull it's data from this new variable. This is the part of code that does the trick:
//Declaration
SqlCommand cmd = new SqlCommand();
SqlConnection conn;
SqlDataAdapter SDA = new SqlDataAdapter();
//In the button which triggers the refreshment
cmd.Connection = conn;
cmd.CommandText = @"SELECT * FROM MarpromTablica_v_1_1";
SDA.SelectCommand = cmd;
SDA.Fill(spremnik);
dataGridView1.DataSource = spremnik.Tables[0].DefaultView;
Thanks to everybody who commented, it's been a lot of help.
Sam HobbsPosted Nov 21, 2011, 4:24 PM
Vineet Kumar SainiPosted Nov 21, 2011, 4:12 PM
You have to do one of the following:
1. Add new value to DataGridViews DataSource. If the DataSource is a BindingList, the grid will be automatically notified of the new value. If not, use a BindingSource and invoke "ResetBindings".
2. Repopulate the DataGridView DataSource from the database.
thanks...
Sam HobbsPosted Nov 21, 2011, 3:13 PM
Neven DraskovicPosted Nov 21, 2011, 9:44 AM
myDataTableDataAdapter.Fill(myDataSet.myDataTable)
Where myDataTable is a table in a database, and myDataSet is connected to it. If I'm correct, I am missing a step in my code - I had some problems with connecting my program to a database, but I finally figured it out, so I am 100% sure that the data I input from my form is indeed stored in my database (I managed to do an ExecuteQuery and it produced a correct result). So database is getting it's data correctly (that would be step 1) and whenever I close and run my application, the newly added data is displayed correctly. I added the functions myDataSet.AcceptChanges and .GetChanges and after that the boolean HasChanges - I wanted to get the message if the DataSet was changed in any way - but I never got that message, so obviously I missed one step.
Any advice on what that step might be, and how do I make it work?
Sam HobbsPosted Nov 18, 2011, 4:20 PM
I assume you are not using the Entity Framework; in other words, your data source is a TableAdapter. For anyone that want more information about that, one place to start is my Database Table Update in a DataGridView without Writing Code.
Are you using the TableAdapter to add data? Probably not. If you did, then the data in the data source would be automatically updated and the DataGridView would be be automatically updated. I forget the details of how to use a TableAdapter to insert data but I can refer to my programs if necessary. The general idea is that there is a Rows collection and you simply add new rows to the Rows collection and then you call a function to write the modifications to the database. If you do that then the records will be added efficiently and the controls wil also be updated as you expect.
If you need clarification of any of that, then please state explicitly if your data source uses a TableAdapter or the Entity Framework.
Neven DraskovicPosted Nov 18, 2011, 8:26 AM
Also, I see you are using SqlClient methods - I'm trying to avoid it because when I try to connect to my database with connection string I get an exception message size of a house. I think something is wrong with my SQL Server 2005 installation, but unfortunetly I cannot change that.
Can you think of another way to do this
Satyapriya NayakPosted Nov 18, 2011, 8:16 AM
Can your share your code.
Try this...
private void btninsert_Click(object sender, System.EventArgs e)
{
con.Open();
str = "insert into student values('" + txtid.Text.Trim() + "','" + txtname.Text.Trim() + "'," + txtmarks.Text.Trim() + ",'" + txtaddress.Text.Trim() + "','" + txtyear.Text.Trim() + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show ("Records Successfuly Inserted");
bindgrid();
}
void bindgrid()
{
con.Open();
str="select * from student";
com=new SqlCommand(str,con);
sqlda=new SqlDataAdapter(com);
ds=new DataSet();
sqlda.Fill(ds,"student");
dataGridView1.DataSource=ds;
dataGridView1.DataMember=("student");
con.Close();
}
Thanks
Neven DraskovicPosted Nov 18, 2011, 8:03 AM
ProbniDataContext PDC = new ProbniDataContext("addres of database");
In button1_click method (that is used to execute the command):
PDC.ExecuteCommand(INSERT INTO....)
This part works fine, when I click on a button and exit the application, I can see my entry in the database, and if I close and restart the application the data shows up in the DataGridView as expected.
For the second part - I'v made a DataGridView that uses a table from that database (it is a simple database, has only one table) as DataSource, and whenever I start my application, it takes the data and displays it. The problem I'm encounterig is that when I add a new entry during runtime, the DataGridView doesent respond to it. I'v added a second button that should tell the DataGridView control that there is new data in database and that it should refresh it self. Now, I know that dataGridView.Refresh() only redraws the control, it doesent do anything to the datasource and its contents.
My question is - how can I make the DataGridView display the data I'v added during runtime - I expect that I have to refresh it's Datasource, but I don't know how to do it.
Gomathi PalaniswamyPosted Nov 18, 2011, 7:47 AM