Problem in ToolStripButton (.Net 2008, Framework 3.5).
-
Hi, I have an editable datagridview that's been databound with dataadapter and datatable, and a normal button on a form. When i edit the dgv's cells and click the save button it fires the datagridview_cellvalidating event and if the cell validation is correct it updates and saves the current record to the datasource. Also, before save it commits change in any cell under editing. But when I use a toolstripbutton to do the same work, though it saves the dgv records but doesn't update the current change i.e. it saves the old records that are being fetched when the dgv was populated first during form load. Also, clicking the toolstripbutton doesn't fire datagridview_cellvalidating, datagridview_rowvalidating etc events. What it seems to be is it's not commiting the change in any cell under editing. Example- Dgv While load: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Dgv while edit but before clicking toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 Tim Yes C03 Alex Yes Dgv after clicking the toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Why isn't it happening and how can I make it work? Please Help. Please also let me know how can I fire datagridview_cellvalidating, datagridview_rowvalidating and other events; and commit cell value change by clicking toolstripbutton. Regards.
-
Hi, I have an editable datagridview that's been databound with dataadapter and datatable, and a normal button on a form. When i edit the dgv's cells and click the save button it fires the datagridview_cellvalidating event and if the cell validation is correct it updates and saves the current record to the datasource. Also, before save it commits change in any cell under editing. But when I use a toolstripbutton to do the same work, though it saves the dgv records but doesn't update the current change i.e. it saves the old records that are being fetched when the dgv was populated first during form load. Also, clicking the toolstripbutton doesn't fire datagridview_cellvalidating, datagridview_rowvalidating etc events. What it seems to be is it's not commiting the change in any cell under editing. Example- Dgv While load: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Dgv while edit but before clicking toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 Tim Yes C03 Alex Yes Dgv after clicking the toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Why isn't it happening and how can I make it work? Please Help. Please also let me know how can I fire datagridview_cellvalidating, datagridview_rowvalidating and other events; and commit cell value change by clicking toolstripbutton. Regards.
-
use dataGridView.CommitEdit() or EndEdit() to update the cell value as first thing on button click. These function may also cause your validation events to fire (but I am not 100 % on that)
Life goes very fast. Tomorrow, today is already yesterday.
I'v already tried both of them but of no use. I think toolstrip is an external band to the form. That's why any action performed on it is not reflected on the form. I'm still looking for it....
-
I'v already tried both of them but of no use. I think toolstrip is an external band to the form. That's why any action performed on it is not reflected on the form. I'm still looking for it....
I don't think that is your problem. I have manage to use a tool strip button to confirm a table edit and it worked for me. Are you sure you have your tool strip button event set to the correct function?
Life goes very fast. Tomorrow, today is already yesterday.
-
I don't think that is your problem. I have manage to use a tool strip button to confirm a table edit and it worked for me. Are you sure you have your tool strip button event set to the correct function?
Life goes very fast. Tomorrow, today is already yesterday.
Here's an interesting thing I found. The toolstripbuttons are notorious to be evaluated first before Datagridview. So, if u edit a cell and commit it by going to a different cell or the next row and then press the toolstripbutton to save, it's working fine and can perform the validations, update, save etc. But when u press the toolstripbutton while editing a cell and not committing it manually, there's where it is failing. A workaround can be, write all the requisite codes in a standard button click event, hide the button beneath any other control, and in the toolstripbutton's click event write- StandardButtonName.PerformClick N.B.- My datagridview updates only after clicking the save button. Please let me know ur opinions on this. Thanks.
-
Here's an interesting thing I found. The toolstripbuttons are notorious to be evaluated first before Datagridview. So, if u edit a cell and commit it by going to a different cell or the next row and then press the toolstripbutton to save, it's working fine and can perform the validations, update, save etc. But when u press the toolstripbutton while editing a cell and not committing it manually, there's where it is failing. A workaround can be, write all the requisite codes in a standard button click event, hide the button beneath any other control, and in the toolstripbutton's click event write- StandardButtonName.PerformClick N.B.- My datagridview updates only after clicking the save button. Please let me know ur opinions on this. Thanks.
OK I have dug out my code for you that works just fine.
void ToolStripButtonClick(object o, EventArgs e)
{
ClickFunction();
}void ClickFunction()
{
dataGridViewMain.EndEdit();//now getting the value of any cell (including one in selection/edit) returns the actual displayed value
}So... ToolStripButtonClick event fired ClickFunction called EndEdit called Now can get correct value ..if this is not working for you then very strange EDIT: Are you getting your value from the datasource? Maybe that is the issue. Can you force an update of that?
Life goes very fast. Tomorrow, today is already yesterday.
-
OK I have dug out my code for you that works just fine.
void ToolStripButtonClick(object o, EventArgs e)
{
ClickFunction();
}void ClickFunction()
{
dataGridViewMain.EndEdit();//now getting the value of any cell (including one in selection/edit) returns the actual displayed value
}So... ToolStripButtonClick event fired ClickFunction called EndEdit called Now can get correct value ..if this is not working for you then very strange EDIT: Are you getting your value from the datasource? Maybe that is the issue. Can you force an update of that?
Life goes very fast. Tomorrow, today is already yesterday.
Ok I'm gonna cross check my code once again for any bug overlooked and let u know.
-
OK I have dug out my code for you that works just fine.
void ToolStripButtonClick(object o, EventArgs e)
{
ClickFunction();
}void ClickFunction()
{
dataGridViewMain.EndEdit();//now getting the value of any cell (including one in selection/edit) returns the actual displayed value
}So... ToolStripButtonClick event fired ClickFunction called EndEdit called Now can get correct value ..if this is not working for you then very strange EDIT: Are you getting your value from the datasource? Maybe that is the issue. Can you force an update of that?
Life goes very fast. Tomorrow, today is already yesterday.
No way, I'm still in darkness. Here's my entire code. please check where's my mistake.
Private strSql As String
Private da As SqlDataAdapter
Private bs As BindingSource
Private dtbl As DataTablePrivate Sub PopulateDgv(Optional ByVal strQuery As String = Nothing) Dim bs As BindingSource Dim cb As SqlCommandBuilder Try RoomPriceID = New DataGridViewTextBoxColumn PeriodDate = New DataGridViewTextBoxColumn Day = New DataGridViewTextBoxColumn Cost = New DataGridViewTextBoxColumn Price = New DataGridViewTextBoxColumn Allotment = New DataGridViewTextBoxColumn Notes = New DataGridViewTextBoxColumn dgvPriceList.Columns.Clear() dgvPriceList.DataSource = Nothing dgvPriceList.Columns.AddRange(New DataGridViewColumn() {RoomPriceID, PeriodDate, Day, Cost, Price, Allotment, Notes}) RoomPriceID.Name = "RoomPriceID" RoomPriceID.DataPropertyName = "RoomPriceID" RoomPriceID.Visible = False PeriodDate.Name = "PeriodDate" PeriodDate.DataPropertyName = "PeriodDate" PeriodDate.ReadOnly = True PeriodDate.HeaderText = "Date" PeriodDate.Width = 100 Day.Name = "Day" Day.DataPropertyName = "Day" Day.ReadOnly = True Day.HeaderText = "Day" Day.Width = 100 Cost.Name = "Cost" Cost.DataPropertyName = "Cost" Cost.HeaderText = "Cost" Cost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Cost.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight Cost.Width = 100 Price.Name = "Price" Price.DataPropertyName = "Price" Price.HeaderText = "Price" Price.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Price.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight Price.Width = 100 Allotment.Name = "Allotment" Allotment.DataPropertyName = "Allotment" Allotment.HeaderText = "Allotment" Allotment.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Allotment.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight Allotment.Width = 100 Notes.Name = "Note
-
No way, I'm still in darkness. Here's my entire code. please check where's my mistake.
Private strSql As String
Private da As SqlDataAdapter
Private bs As BindingSource
Private dtbl As DataTablePrivate Sub PopulateDgv(Optional ByVal strQuery As String = Nothing) Dim bs As BindingSource Dim cb As SqlCommandBuilder Try RoomPriceID = New DataGridViewTextBoxColumn PeriodDate = New DataGridViewTextBoxColumn Day = New DataGridViewTextBoxColumn Cost = New DataGridViewTextBoxColumn Price = New DataGridViewTextBoxColumn Allotment = New DataGridViewTextBoxColumn Notes = New DataGridViewTextBoxColumn dgvPriceList.Columns.Clear() dgvPriceList.DataSource = Nothing dgvPriceList.Columns.AddRange(New DataGridViewColumn() {RoomPriceID, PeriodDate, Day, Cost, Price, Allotment, Notes}) RoomPriceID.Name = "RoomPriceID" RoomPriceID.DataPropertyName = "RoomPriceID" RoomPriceID.Visible = False PeriodDate.Name = "PeriodDate" PeriodDate.DataPropertyName = "PeriodDate" PeriodDate.ReadOnly = True PeriodDate.HeaderText = "Date" PeriodDate.Width = 100 Day.Name = "Day" Day.DataPropertyName = "Day" Day.ReadOnly = True Day.HeaderText = "Day" Day.Width = 100 Cost.Name = "Cost" Cost.DataPropertyName = "Cost" Cost.HeaderText = "Cost" Cost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Cost.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight Cost.Width = 100 Price.Name = "Price" Price.DataPropertyName = "Price" Price.HeaderText = "Price" Price.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Price.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight Price.Width = 100 Allotment.Name = "Allotment" Allotment.DataPropertyName = "Allotment" Allotment.HeaderText = "Allotment" Allotment.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Allotment.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight Allotment.Width = 100 Notes.Name = "Note