Below in image i have Reason as combo box which has three options .
On selecting first value such as In house Rework, Second column Responsible party which is
also combo box should get populated with list collection and so like on selection
of another value from Reason combo box Responsible party should get populated with
another list collection.
Since i have attempted on end edit of gridview and it's happening to show value but if on next row i change my Rework combo box value the previous value of Responsible party doesn't retain its value.
So how can i achieve to prevent this case.
So any help is appreciated.
Are you trying to bind the other two combo boxes based off the value selected in the reason? If so I would use the CellValueChanged event in the Data Grid's events and you can programmatically build a list and databind it. If this is what you want to do I'll post an example here, if not please clarify so I can be help.
Private Sub dgVendors_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgVendors.CellValueChanged
If dgVendors.Columns(e.ColumnIndex).HeaderText = "Reason" Then
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DataSource = Nothing
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DisplayMember = ""
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).ValueMember = ""
ElseIf dgVendors.Columns(e.ColumnIndex).HeaderText = "Responsible Party" Then
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DataSource = Nothing
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DisplayMember = ""
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).ValueMember = ""
End If
End Sub
If you want to handle the selection change event of the dropdown, you can handle the EditingControlShowing event of the DataGridView.
There is also an MSDN page which might help you out.
Also, make sure you change the DataSource of the DataGridViewComboBoxCell on the CurrentRow, not of the DataGridViewComobBoxColumn.
Related
I am using the DataGridView Control for reading and writing an XML file through XML Serialization.
I have an issue as explained below:
I read an XML file and populate DataGridView controls with the deserialized object.
I update all the values on the DataGridView on the cell.
I choose the File Save As option without losing focus on the last cell.
After this, the value of the particular cell is not updated. If I intentionally shift focus away (say I click on another cell on the same grid) the value is updated.
Can anyone suggest any solution for this?
The best way (though quick and dirty) is to assign the currentCell value to Nothing.
For example, in the save method, do:
dgvMyGrid.CurrentCell = Nothing
and then proceed further.
It's because the edited cell value is not committed to the DataSource until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler :
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
If I understand you correctly, a cell is in editing mode and you're trying to programmatically stop editing and pass the value to the underlying datasource?
I'm using a somewhat "dirty" approach to do that in one of my applications:
if (dataGridView1.CurrentCell.IsInEditMode)
{
int y = dataGridView1.CurrentCellAddress.Y;
int x = dataGridView1.CurrentCellAddress.X;
if (y > 0)
dataGridView1.CurrentCell = dataGridView1.Rows[y - 1].Cells[x];
else
dataGridView1.CurrentCell = dataGridView1.Rows[y + 1].Cells[x];
dataGridView1.CurrentCell = dataGridView1.Rows[y].Cells[x];
}
That piece of code first checks whether the current cell is in edit mode. Then it changes the current cell programmatically (either to the previous row or the next row in case we're in the first row). After that, it restores the current cell selection.
You would call this code in your "File Save As" handler.
I had the same situation and I was even using accelerator keys for save button for saving the grid values. When I click on Save button focus lost from DGV and hence cell value is committed, but when I use accelerator keys focus doesn't lost from DGV hence no committing of the cell value.
After looking at the Amit Karmakar answer out of curiosity I tried that answer and it worked. To find out more details I went into debugging of the DGV and found that it is really same thing as commitedit which somehow doesn't work if you use it in the save button click.
When we set CurrentCell of DGV to null, before setting it to null DGV first gets the edited value and pushes it in to cell value and then sets CurrentCell REFERENCE to null. Here it doesn't mean that it is setting underlying DGV cell to null. Hence this works perfectly for the above problem.
Note: This solution may not work perfectly when you have validating events for the cell and if user enters invalid data which will fail validation. In this case setting current cell to null also fails as it cannot push the value to cell.
I gave this explanation as I've raised question on Amit Karmakar answer asking how can it be possible. I thought it may help some other, so dropped this explanation as answer.
OK, this is UGLY but it works to get the FINAL CHANGES from the grid WITHOUT having to move to another row:
With DataGridView1
.DataSource = Nothing
.DataSource = gridDataTable
Dim changedFoo As DataTable = gridDataTable.GetChanges
End With
However I still like the answer from Amit Karmakar the best.
I've added the 'DataGridView1.CurrentCell = Nothing' to the DataGridView1 LostFocus event.
You can get the value of a cell which is not yet committed using the EditedFormattedValue property for the current cell, as below
dataGridView1.CurrentCell.EditedFormattedValue
I have a form containing a DevExpress LookUpEdit (Windows Forms) which is bound to a list of objects with several displayed properties. The EditValue property is set to another object's property which will receive the selected value.
The user may select any item from the list of objects, but I also want to allow empty selections i.e. the EditValue would become null and the displayed text should be the default [No entry] then.
How could this be accomplished the easiest way?
Currently there's no way to clear the value after it has been set once.
There are two options:
1. User can press Ctrl+Del to clear the value
2. However, this is not intuitive. What I do is add another value to the bound list.
var list = GetOriginalList(); // <- get all possible values
list.Add(new MyItem("[empty]", null)); // <- display name and ID
Try this one :
In form load :
LookUpEditName.Properties.AllowNullInput = true ;
LookUpEditName.Properties.NullText = "No entry";
and use LookUpEditName.EditValue = null; to clear the value
Add Cancel Button to your Lookup Edit and add reset code in Button Click event
Dim editor As LookUpEdit = CType(sender, LookUpEdit)
If editor.Properties.Buttons.IndexOf(e.Button) = 0 Then
YourLookUpEdit.EditValue = DBNull.Value
End If
DataGridViewCheckBoxCell is not getting checked. I have inserted the first column like this
DataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn { Name = "Print" });
This is not working
DataGridView1.Rows[0].Cells[0].Value = true;
Neither this is working
DataGridView1.Rows[0].Cells[0].Value = cell.TrueValue;
What could be the reason it is not getting checked?
I was having issues with this myself. I was using the following code, and I couldnt figure out why it wasn't working right all of the time.
(Note that my code is in VB, but I think the same concept would apply to other .NET languages)
Dim check As DataGridViewCheckBoxCell = DataGridView1.Rows(i).Cells("columnNameHere")
If check.Value = check.TrueValue Then
'do stuff
End If
I then realized that it was because the underlying value of the cell isnt changed until it loses focus. I think DGVs always behave like this, but it can be easy to forget.
My solution was to simply add a little bit of code to the data grid views on click event handler. When the user clicks the checkbox, all it does is shift the focus elsewhere. I shifted the focus to a label so that it doesnt have any unintended consequences. (Ex: if you shift it to a button the user might be surprised when they press enter and a random button activates.) Depending on what else is in your DGV, you may want to check the column index of the event so that you are only doing this for the checkbox columns. All of the other columns in my DGV are read only anyways, so it didnt matter in my case.
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Label1.Focus()
End Sub
If the code in question was contained in a button, the focus would already be shifted away from the DGV to the button, so this wouldn't be an issue. In my case, the code was activated by a timer - meaning that the DGV cell in question wouldn't necissarily lose focus before the timer fired.
I don't know why it is not working for you, but you can try following method instead.
dataGridView1.Rows[0].SetValues(true);
This will only check the first item. When you want to set values for more cells, just use more parameters.
var values = new bool[] { true, false, true };
dataGridView1.Rows[0].SetValues(values);
This will check the first and the third cell, but the second cell will remain unchecked.
your first line of code actually worked for me:
DataGridView1.Rows[0].Cells[0].Value = true;
checked the first line. the second line isn't working because there is no meaning to cell.TrueValue, and a cell's property TrueValue is not a const of a checked check box
let me just add that the way you address your DataGridView's properties is not very safe and can cause exceptions
Is your datagridview Edit Mode property is set to Edit Programmatically ?
I was having the same issue, So I changed my datagridview Edit Mode property to Edit on Enter. Now it's working fine for me.
Please set your datagridview Edit Mode to Edit on Enter.
If you want to make other (Non Checkbox) columns read only use DataDridView1.Column("Column Name Here Or Index").Readonly = True to made them read only.
Sorry For Late, But other searchers can get some helps from this answer.
If you just want to add new checkbox column and want to check`uncheckcheckboxes in column you need to addTrueValueandFalseValuefor yourCheckBoxColumn`. Go to the Edit Columns dialog and set TrueValue and FalseValue attributes for your column.
Thanks.
well i have worked with datagridview now i need to work with a listview i dont know how to select the value it has in one cell..
my another option is when a cell was added or deleted, all this information is on datagridview
but it doesn't have a (datasourcechanged) then how can i anyone of these?
I tried secund with this code:
DataGridView1.DataSource = lvDevices
so i could select anyone but i need when in datagrid changed its value, it passed to datagridview well my another way is
how can i select the value of one cell in a ListView?
you could try this in this listview1_doubleclick event. ...
string a = listView1.SelectedItems[0].SubItems[column index corresponding selected cell].Text.
I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx