Datagridview : how to reload the data? - winforms

I have a populated Datagridview, which gets its data from a DataTableAdapter.
I have a separate button on my form. When I click the button I want the DataTable to refresh and repopulate completely.
Seems so easy, but I can't find a way to do this.
Thanks in advance

If you wish to swap the code for dynamic grid functionality, right click on your datatable and add a new query (green box in the SS):
Select * from tablename
Then go locate the line in the form_load that takes care of the data population:
this.yourTableAdapter.Fill(this.yourDataSet.yourdatatable);
The default SQL query is always called .Fill, but in case you add a new query the second one will be called (orange box in SS):
this.yourTableAdapter.FillBy(this.yourDataSet.yourdatatable);
By running this second method somewhere in your code (a button press maybe) the grid should change to the new sql command and display the new results in the gridview:
protected void Button1_Click(object sender, EventArgs e)
{
this.yourTableAdapter.FillBy(this.yourDataSet.yourdatatable);
}

Related

How to commit changes to local SQL DB In Visual Studio 2015?

I am new to working with databases. I made a simple form app to display this data table however, I can't get the data into the database so that the form displays it. I initially had only 1 employee listed here and he shows up in my application. I just added the last 3 employees into this table view but if I run the app, only the original employee (id 1) shows up. How do I commit the last 3 employees' info into the database? I Googled some info on this and found a select * from dbo.EmployeeInfo query and ran it, it displays the employee info right but it doesn't seem to change the database with the new info. Thank you.
I was stuck here too.
What I did was -
1) Updated the values and after updating the last values, I pressed Enter
It seems to work for me.
Enter button on keyboard makes life simple. :)
I created a form with a GridView, and binded it with a Datasource it shows me the data in the form u showed above...
Added some textboxes for input......
The code below shows how it binds the datasource with the gridview and how you would insert data permanently using input from textboxes at a button click event
private void Form1_Load(object sender, EventArgs e)
{
// using a gridview by binding the dataset with it at form load
//It creates the table for you with the relevant columns
//and fills it with this line of code
this.employeesTableAdapter.Fill(this.practiceDBDataSet.Employees);
}
private void btnSubmit_Click(object sender, EventArgs e)
{
//on add user button click
//I access the tableadpater created by the dataset to be accessed by the gridview
//the txt.... represents my fields names according to the database
employeesTableAdapter.Insert(txtName.Text, txtSurname.Text, txtAge.Text, Convert.ToInt32(txtAge.Text));
}
And then the txt.... are in place for the data required by the Database, where you would be getting your input from.........

Unable to set datagridview column to bold after data loaded in datagridview

I am able to make particular column data bold but Unable to set datagridview column data to bold after data loaded in datagridview.
I am using below code to make it Bold which is working if I Set this in columnAdd event but unable to change after data loaded in to the datagridview.
dataGridView1.Columns[1].DefaultCellStyle.Font = new System.Drawing.Font("Roboto", 8.5F, FontStyle.Bold);
please help if its possible.. I am using windows form DataGridView
2017Button || 2010Button || 2005Button
On clicking particular button I need to highlight that particular column. For Example, My data contains 3 Columns 2017 Data, 2010 Data, 2005 Data, Which Year I Select from above button out of 2017,2010,2005 button, that column should get bold.. And also I don't want to rebind the data to gridview
You must try it on below event
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.Columns[1].DefaultCellStyle.Font = new System.Drawing.Font("Roboto", 8.5F, FontStyle.Bold);
}

I have a Mouse Click Event handler which works when i select the first time but..?

I have a DataGridView in C# which is connected with my Access database. I see all the books on my DataGridView. I have created a mouse Click event handler to select a row than I can save that row, as you see below:
private void Mouse_Click(object sender, MouseEventArgs e)
{
try
{
temp = dataGridView1.SelectedRows[0].Index.ToSt…
//This one is selecting the row. To do that select panel>event> on MouseClick write name and double click:
MessageBox.Show("You have selected the row " + temp);
}
catch
{
}
}
It works when I open the form. However, after I save the selected row to another database, the Mouse Click event handler stop working. In other words, after saving the first selected row, when I come back to select another row and save to database the Mouse Click Event handler does not work. The Message says row is out of range.
You didn't give too much information related to connection type, Event Handler and more importantly the way you save/update the record so I'm just guessing here: aren't you accidentally overwriting the DataSource of your grid? That's the only thing I can imagine for such behavior ("row is out of range" - which also assumes the event still works as intended but the code in the function cannot see the data anymore).

WPF CellEditing issue - reload datagrid rows

I have datagrid and one panel. When I click on any row, all data should be appeared on that panel. And when I edit cell and after editing if I click another cell of same row, panel should be updated immediately. My datagrid is bound through item source (data table), so if I make any change to grid (add/delete/edit) my item source is updated and as per item source Panel is updated.
To achieve cell edit thing I use following code and it's working.
void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (!isManualEditCommit)
{
isManualEditCommit = true;
DataGrid grid = (DataGrid)sender;
grid.CommitEdit(DataGridEditingUnit.Row, true);
isManualEditCommit = false;
}
}
Issue: Only issue is when this line gets called (grid.CommitEdit), it basically loading all rows again.and if datatable is really big than it takes few sec to load all rows. If I don't commit grid then my changes of datagrid appears on panel after I click on another row. I want to achieve it when i click on another cell of same row without loading rows again.
Here is Image
Pls help
Thanks
this is a common pitfall of the DataGrid : the commit logic. The solution i found (it was booleans, not numbers) was to make my own custom DataGridColumn, with my bindings triggers set on PropertyChanged, to have the content updated at once and not only on commit. Surely this is a little work, but DataGrid standards column won't allow you to change that commit logic.

How to update database using dataset, tablemanager, and bindingsource?

This is a pretty small program, so I just used the autogenerated code for my datasource and dragged/dropped a datagridview on my WinForm. I'm trying to manually update a column in a row, but it fails each time I get to the "UpdateAll" of my tablemanager. Here's my code for double click of cell content:
private void dgv1DataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
string delMemberID = dgv1DataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
ds1DataSet.Table1.Rows[ds1DataSet.Table1.Rows.IndexOf(ds1DataSet.Table1.FindByMemberID(delMemberID))][9] = MemberID.ToString();
this.Validate();
this.bs1BindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.ds1DataSet);
MessageBox.Show("Replacement Successful");
this.Close();
}
catch
{
MessageBox.Show("Replacement Failed");
}
}
My expectations are; when a user double clicks anywhere inside a row of the datagridview, it will update that row with a different "MemberID" for that column, save the data everywhere (including database), and then close the form (to return the parent form).
For some reason, my tableAdapterManager wasn't linked to my TableAdapter on that form - once I linked them, the above code worked fine.
To be more precise, I clicked on the design view of my form and clicked the the tableAdapterManager at the bottom of the design window. Then looking in the "Properties" window, I noticed that the TableAdapter was blank (if you used a wizard to create the DataSource, then it will be named the same as the table in your DataSource). Once I selected the correct name for the TableAdapter, everything worked perfect.

Resources