Suppose if I say I have selected a row in gridview and want that row to come up in to their respective textboxes. For example, I have selected a row which has First Name and Last Names and on selection of row, the data from gridview should come in to textbox on winform. Please tell me.
I am guess it is something based on selection changed event if I am right ? Like below:
private void dgv_SelectionChanged(object sender, EventArgs e)
{
string str = txtFirstName.Text;
DataGridViewRow selectedRow;
}
If I got your question right, your solution is to use DataGridView.SelectedRows Property along with DataGridView.SelectionChanged Event
DataGridView.SelectedRows Gets the collection of rows selected by the
user.
DataGridView.SelectionChanged occurs whenever cells are selected or
the selection is canceled, whether programmatically or by user action.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_FirstName.Text = row.Cells(0).Value.ToString; // or row.Cells["ColumnName"].Value;
}
}
}
MSDN and MSDN
Related
I'm using SelectionChanged event of a DataGridView. I have a code that will display the value of a DataGridView's cell to a TextBox. However, when I click the column header, this also triggers the code (I assume because of the SelectionChanged event) and will display an error.
What I want to achieve is to enclose my codes in the datagridview1_SelectionChanged in an if statement. Wherein:
if(column header is clicked)
//don't do anything`
else
//do the display of data to textbox`
I just want to know the code for checking if you clicked a header or not.
just check in the event
List_SelectionChanged(object sender, EventArgs e)
{
if(e.Rowindex>-1)
...
}
-1 is for header
You could check your DataGridView's CurrentRow for null. If it is not null it means that you've not clicked the column header:
private void yourDataGridView_SelectionChanged(object sender, EventArgs e)
{
var current = yourDataGridView.CurrentRow;
if (current != null) // Means that you've not clicked the column header
{
//Display the value of a DataGridView's cell to a TextBox
}
}
I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}
My motive is to display some information as link and on click of that, I should be able to get id of clicked item and open new window with detailed information of item. As i am new in win forms but i did some research, Possible option for this might be DataGridViewLinkColumn but i am not able to link id with column data and click event on which open new window.
Or there any other better approach possible.?
When you have a datagridview you can get the values of a cell as follows:
Firstly, create a cellclick event
datagridview1.CellClick+= CellClickEvent;
DataGridViewCellEventArgs holds some properties, these would be rowindex (row you clicked) and columnindex (column you clicked) and some other...
make a datagrid with 2 columns, column 0 holds the Id of the row, column 1 holds the link
void CellClickEvent(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == 1) // i'll take column 1 as a link
{
var link = datagridview1[e.columnindex, e.rowindex].Value;
var id = datagridview1[0, e.rowindex].Value;
DoSomeThingWithLink(link, id);
}
}
void DoSomeThingWithLink(string link, int id)
{
var myDialog = new Dialog(link,id);
myDialog.ShowDialog();
myDialog.Dispose(); //Dispose object after you have used it
}
I'm assuming you're using a DataGridView element.
What you could do is use the CellClick event of the object. It will have a DataGridViewCellEventArgs object passed on, on which there's a ColumnIndex property and a RowIndex property. This way you could figure out where in the the datagrid the user clicked.
And, for example, you could use that information to look up the id or other info since you now know the row & the cell the user clicked on.
arbitrary e.g.:
// wire up the event handler, this could be anywhere in your code
dataGridView.CellClick += dataGridView_CellClick; // when the event fires, the method dataGridView_CellClick (as shown below) will be executed
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
var rowIndex = e.RowIndex;
var columnIndex = e.ColumnIndex; // = the cell the user clicked in
// For example, fetching data from another cell
var cell = dataGridView.Rows[rowIndex].Cells[columnIndex];
// Depending on the cell's type* (see a list of them here: http://msdn.microsoft.com/en-us/library/bxt3k60s(v=vs.80).ASPX) you could cast it
var castedCell = cell as DataGridViewTextBoxColumn;
// Use the cell to perform action
someActionMethod(castedCell.Property);
}
(*DataGridViewCell types: http://msdn.microsoft.com/en-us/library/bxt3k60s(v=vs.80).ASPX)
I have a datagridview. It has 2 columns which are of type boolean. Now what I want to do is, when I click on one checkbox the other check box in the same row should be unchecked. that is, at a time only one check box should be selected in a particular row. The user should not be able to select the 2 check boxes together.
How can I achieve this ? I tried using cellcontentclick and cellcontentChanged events. nothing works.
any inputs ??
You can use CellEndEdit Event or CellLeave event
when ever you will leave the cell after doing edits this event will fire.
In the following snippet Column1 is your first checkbox and Column2 is your other checkbox.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value))
dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value = false;
}
Use DataGrid_CellEditEnding
In that iterate through DataSource, if u find any checked colum (boolean true) make it unchecked (boolean false).
hope it will help u
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
foreach (var Item in dataGridView1.ItemsSource)
{
if(Item.isChecked)
Item.isChecked = false;
}
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value))
dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value = false;
}
I am using a Datagridview control on winform and a bindingsource for data. Data is filled in bindingsource and accordingly datagridview is populated. I am looking for an event or something like that which will fire up when a row from bindingsource is added to the datagridview.
I want to perform some operations over the added row. I tried with RowsAdded event but e.RowIndex is not being retrieved properly.
Edit1: Let's say I am having 10 records in database table. I am fetching these into bindingsource and using bindingsource as a datasource for Datagridview. Now while adding row to Datagridview, I want to perform some UI operations on the Datagridview. I used RowsAdded event but it is giving me RowIndex as 0 or 1 always. I also tried a foreach loop over RowsCount, and if I debug the code, the execution flow is as per the expectations, but on the UI it is not getting reflected. I have called Datagridview1.refresh() after everything is done.
Can you please help me out to get this ?
When the user adds a new row using the row for new records, the DataGridViewRowsAddedEventArgs.RowIndex value in the handler for this event is equal to the index of the new location of the row for new records, which is one greater than the row just added.
When you add rows programmatically, however, the RowIndex value is the
index of the first row added.
private void dataGridView1_NewRowNeeded(object sender,
DataGridViewRowEventArgs e)
{
newRowNeeded = true;
}
private void dataGridView1_RowsAdded(object sender,
DataGridViewRowsAddedEventArgs e)
{
if (newRowNeeded)
{
newRowNeeded = false;
numberOfRows = numberOfRows + 1;
}
}
will fetch you the exact row refer msdn rowadded link
Depending on the operations you wish to perform after binding you could use Control.BindingContextChanged event to iterate on the grid rows:
private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
{
foreach (var row in dataGridView1.Rows) {
}
}
If it's not working could you say what exactly are you trying to accomplish after binding?