Grid view Edit and save to database Winform - c#

For a windows application, I have a form with a gridview. In the form, I have an Edit button. If the user selects one row and clicks on Edit, it has to redirect to another form with the selected data. The user can then enter details in that. When the user clicks on Save Data, it should save and go back to the database.
FIRSTListForm
private void btnNewFIRSTList_ItemClick(object sender, ItemClickEventArgs e)`
{
FIRSTEditForm FIRSTEditForm = new FIRSTEditForm(FIRSTID);`
FIRSTEditForm.Show();
}
private void btnEditFIRSTList_ItemClick(object sender, ItemClickEventArgs e)
{
//I need to know which row I selected before to Edit button
//object IdFirst = ((GridView)sender).GetRowCellValue(e.RowHandle, "IDFIRST");
//I need like a GetRowCellValue
FIRSTEditForm.Show();
}

You need a save method to write your data back to your database and call it on your save button click
Here is multiple examples of exactly what your wanting
if you want to open a separate form then youll need something like This

If you're using Datasets with winforms you can try:
1)Check if your gridview has any selected rows first
if (gridView1.SelectedRowsCount > 0)
{
......
}
2) If that's the case using strongly named DataRow class and BindingSource.Current:
YoutDataSet.YourTableOrResultSetRow Row;
var P = YourTableOrResultSetBindingSource.Current as DataRowView;
Row = (P.Row as YoutDataSet.YourTableOrResultSetRow );
int id = Row.ColumnNameThatContainstheID; //this will give you the ID you're after

Try it sir,
private void gridView_DoubleClick(object sender, EventArgs e)
{
if (gridView.GetFocusedRow() == null) return;
if (gridView.GetFocusedRowCellValue("ID") == null) return;
string id = gridView.GetFocusedRowCellValue("ID").ToString();
FIRSTEditForm frm = new FIRSTEditForm(id);
frm.Show();
}
In FIRSTEditForm use got ID and bind all your controls and save to database.
I use gridView_DoubleClick instead of your btnEditFIRSTList_ItemClick.
Hope it solves!

Related

Update database through TableAdapter

I'm using the table adapter in C# where I created a DataGridView. When I double click a row, I am displaying all the cells in the row in textboxes in a different Form. After I edit the textboxes and click my save button, I would like to take the values from the textboxes and replace them in the database. I can see the changes on the DataGrid, however I am not able to save them in the database.
private void InventoryData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//make new form and display them there
ViewInventoryItem ViewItem = new ViewInventoryItem();
ViewItem.textBox1.Text = this.InventoryData.CurrentRow.Cells[1].Value.ToString();
ViewItem.textBox2.Text = this.InventoryData.CurrentRow.Cells[2].Value.ToString();
ViewItem.textBox3.Text = this.InventoryData.CurrentRow.Cells[3].Value.ToString();
ViewItem.ShowDialog();
if (ViewItem.DialogResult == DialogResult.OK)
{
//save button was pressed
this.InventoryData.CurrentRow.Cells[0].Value = ViewItem.textBox1.Text;
this.InventoryData.CurrentRow.Cells[1].Value = ViewItem.textBox2.Text;
this.InventoryData.CurrentRow.Cells[2].Value = ViewItem.textBox3.Text;
this.Validate();
this.InventoryData.EndEdit();
this.booksTableAdapter.Update(this.InventoryDataSet.Books);
}
}
Found the problem. In the Dataset the Table had only Fill and Get Data. Even after selecting the Wizard to add the other statements it was failing. After adding the UpdateQuery manually, the code works without issues. Thank you Crowcoder for pointing me in the right direction.

Datagridview Button Programming

i am programming a piece of software for a loyalty scheme. I have basically finished all of it. The only thing i am stuck on for ages is programming the buttons for the datagridview which i am using for my redeem offers page. I have gotten it to add a button to the end of each row automatically when you add a new offer but i am clueless on how to program it to recognize each row.
for e.g.)
Row1) Offer 1 is X name and costs Y
Row2) offer 2 is Z Name and costs F
I can only seem to find the ability to program every single button at once and not individual and even so, i need it to work automatically so when i can add a offer i can immediately purchase it. How would i get it to recognize which row i am on?
here is what my form and database looks like.
Picture Of Form - (picture link)
What my database looks like - (picture link)
below ur class
make public int id;
//adding button
DataGridViewButtonColumn col = new DataGridViewButtonColumn();
col.UseColumnTextForButtonValue = True;
col.Text = "ADD";
col.Name = "Btn_add";
DataGridView1.Columns.Add(col);
//pass id values here to do functions..
private void Btn_add_Click(object sender, EventArgs e)
{
}
//this gets selected index of selected row value
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
int id = DataGridView1.SelectedRows(0).Cells(0).Value;
}
In Datagrid please bind OfferId in buy button. When press button then automatically gives id onclick event.
You can simply hook into the RowDataBound Event. This event is called, each time a new entry is put into the table. At this point, you can add a button to the tabe and add a custom action/url to the url.

How to get current row of datagridview values when clicking on button?

I have a View Model in which my data is stored to be used on the presenter and window. However, I now need to get (when pressing a button) the current row's results on the grid into my view model. I first used the mouse double click event, but the requirements changed from that to a button on the form:
private void dgvResults_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (this.colOrderNo.Index != e.ColumnIndex || e.RowIndex < 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) this.dgvResults.Rows[e.RowIndex].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
}
That's my old code. Now I need that here:
private void btnAuditLog_Click(object sender, EventArgs e)
Not sure how to refer to the RowIndex without the DataGridViewCellMouseEventArges event. I tried the CurrentRow property, but it doesn't do the job.
Just another question on the fly, if someone could assist with that as well, how do I get a value from one form to another, spesifically a properties value. It's not on the grid, in a control. It's basically just public string order that's all. I set the value on one form, then need it for query filtering on another. Let me know if you need anything else.
You want to use the SelectedRows property of the DataGridView. It tells which row is selected. In this case, you seem to want the first selected row (since you have a single property called this.AuditOrderKey).
The column order no to longer matters in this context.
if (dgvResults.SelectedRows.Count == 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.SelectedRows[0].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
If you don't have selected rows, CurrentRow is an option:
if (dgvResults.CurrentRow == null) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.CurrentRow.DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
Use the SelectedRows property of the DataGridView. Assuming that you set your DataGridView as MultiSelect = false; and SelectionMode = FullRowSelect;
if (dgvResults.SelectedRows.Count > 0)
{
dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
In your button click event, it will look like this. (Assuming that your button name is btnEdit)
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvResults.SelectedRows.Count > 0)
{
string selectedValue = dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
}

remove default row selection datagridview

I have a datagridview that when it loaded first row is selected. I want to delete a record but I don't want first row select. I want message to user that first select a row then click delete button. I try these code but they don't worked.
//kharidDataGridView.Rows[0].Selected = false;
//kharidDataGridView.ClearSelection();
//kharidDataGridView.CurrentCell = null;
I use below codes in WPF:
object item = datagrideview1.SelectedItem;
if (item == null)
{
//message to user
}
thank you
You can remove default selected row by handling data-grid loaded event
private void datagrideview1_Loaded(object sender, RoutedEventArgs e)
{
datagrideview1.SelectedIndex = -1;
}

A btnclick Function calls another function,but the call not in the btnclick function

consider the following:
code for buttonclick fn in .cs file :
protected void additembtnClick(object sender, EventArgs e)
{
DataTable dt = createTemptable();
dt = (DataTable)Session["dfdtemptable"];
this.DFDLOVlst.DataSource = dt;
this.DFDLOVlst.DataBind();
this.txtLOVCode.Visible = true;
this.txtLOVvalue.Visible = true;
MDIngrdientsCode.Show();
}
this is the on_row clicked fn of the datagrid
protected void OnFindSelect(int Value)
{
}
code for findbtn click:
protected void btnFind_Click(object sender, EventArgs e)
{
this.FindLookup.SetLookup();
this.FindLookup.SearchLookup();
this.MdFindLookup.Show();
}
When the find button is clicked,the lookup control gets loaded with values from database.On selecting any row the selected row values will be added to corresponding text boxs and data grid.
The additembtnclick is to add new entry to the datagrid.The user can add new values or can click the find and select a row and update its values in the datagrid.
Theres no problem when the user adds fresh values .When the user click the find and select a particular row.The values gets add to correspondind fieldls..And now if the user clicks the additem button to add new values to already existing values in datagrid then the fn onfindselected(fn for the onrowclicked event for the lookup control containg datagrid) is called automatically,but the call is not in the button click fn..
Can't figure out whats wrong..?
It appears that theOnFindSelect is not an event handler itself. It is probably being called from another event handler (or more than 1). Try looking for all calls to it and see if that makes sense

Categories

Resources