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

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.

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.........

adding checkboxes to every cell infragistics ultragrid

I'm fairly new to infragistics and i need help -
i need to add a checkbox to every cell in my grid while still displaying the cell value and allow user to check/uncheck the cell-
for ex - my grid has many columns - text, datetime, numbers etc
each cell will display the text/date/number and also have a checkbox for user to check/uncheck that field
IS THIS POSSIBLE?
One possible way is to add an editor in the grid cells. For example, you can add UltraTextEditor with StateEditorButton (on the left or right as is better for your solution) in each cell like this:
private void UltraGrid_InitializeRow(object sender, InitializeRowEventArgs e)
{
if (!e.ReInitialize)
{
foreach (UltraGridCell cell in e.Row.Cells)
{
StateEditorButton checkBox = new StateEditorButton();
UltraTextEditor textEditor = new UltraTextEditor();
textEditor.ButtonsRight.Add(checkBox);
cell.EditorComponent = textEditor;
cell.Column.ButtonDisplayStyle = ButtonDisplayStyle.Always;
}
}
}
Keep in mind this will add many editors to your grid - bad performance. Other possible solution is to add via Creation Filter the check boxes to your cells.
Either way the main question is - how you will save the checked state back to your data source? If you have a boolean column for each column you actually do not need to add any check boxes. So think how you will save this information.

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.

Datagridview : how to reload the data?

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);
}

Resources