How to get the value which is selected in gridview? - c#

When I click on a row I should get the value on which the user is clicked
How do I get that?

How about one of these event:
DataGridView.CellClick
DataGridView.CellContentClick
DataGridView.RowEnter

If I have understood you correctly, then you'll want the SelectedIndexChanged event.

If the grid's SelectionMode is set to FullRowSelect or FullHeaderSelect, you can get the selected row(s), using
DataGridView.SelectedRows
Then for each row you can use the Cells property to get the values.
There are a wide variety of ways to get at the underlying data set.

Related

How to make a specific Column Uneditable In datagridview?

Using a DataGridView, how can I make a specific column uneditable while the grid view itself has "Allow editing" enabled?
Also, how can I execute an event when the selected index in a ComboBox in the DataGridView has changed? Here, ComboBox is a column type.
Another question is, how can I make the header title aligned to the center? I can't find the appropriate property.
You've got a few questions here.
(1) How can I make a specific column uneditable in DataGridView?
Set the ReadOnly flag on the particular column you want to make uneditable.
dataGridView.Columns["YourColumnName"].ReadOnly = true;
(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?
If it's in your DataGridView, it's not a ComboBox; it's a DataGridViewComboBoxColumn. According to MSDN:
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
This one I'm not familiar with, as I've never tried it myself. It appears you want to subscribe to the EditingControlShowing event and then see if something like this works for you (with a little tweaking).
(3) How can I make the header title align in the center?
Set the HeaderCell.Style.Alignment
dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

Programmatically checking checkbox in a gridview

I have a Datagridview with first column as checkbox.I have created the checkboxcolumn at design itself.While updating the gridview according to the entries from database, I have to check and uncheck the checkbox programatically not all at a time but only a specific row.Please tell me how can I update check boxes programmatically.
You can bind the data from database directly to checkbox column if it is bit type in DB.
Do something like this
(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
During bind data you need to check it manually if flag is true then set it checked otherwise unchecked as like
set checkbox1.checked=true or false
Check datagrid prerender event, get reference to the checkbox and set value accordingly.
At the time of databinding use this code
CheckBox chkbx= e.Item.FindControl("CheckBox1") as CheckBox;
then you can manipulate chkbx.Checked to true or false based on your values
and similarly it can be used for rest of the checkboxes buttons

DataGridView Row Enter

I am trying to determine if the user has selected a row to get the currently selected row. But when the form is initialized it calls the event.
Is there a way to check if the data grid view is being initialized? I thought about setting a flag and using RowPostPaint to set the flag.
Any other ways to ignore the select row event during initialization?
Maybe it helps to check if the DataGridView has the focus:
if (this.dataGridView.Focused)
{
// handle selection ...
}
On form initialization the control shouldn't have the focus but when the user manually changes the row selection it should have the focus.
And maybe you are using the wrong event to determine the row selection. Are you using "RowEnter"? I suggest you use "SelectionChanged" and then access the "SelectedRow" property.
It depends to which event you subscribe, I suppose it's SelectionChanged ?
If so - define some boolean variable (like dgvIsInitialized), set it false by default, then load data into datagridview datasource and - after loading - set it true.
And in SelectionChanged event do similar to this:
if (dgvIsInitialized)
MessageBox.Show("Selection changed");

Stuck with datagridview and combo box column

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

How to change visibility of first row in datagridview

I am using datagridview and its datasource is a BindingList.
But when I try to change the visibility of first row, I am getting InvalidOperationException.
How can I change first row's visible value?
Thanks in advance.
I found how to do it.
You must specify:
datagridview.CurrentCell = null;
before setting:
row.Visible = false;
because if currentCell is in the row which is wanted to be hide, then mentioned problem occurs.
regards
Apply a filter perhaps on the DataSource or the grid.
On prerender, in the web domain, set the visible property.
In windows domain, make sure it is not the header row type. You can't hide that, AFAIK. Otherwise just hide it within the ondatabind.

Categories