I have a GridView1 in my form which populate a table from my Database.
The columns in my table are
ID NAME EMAIL ADDS
Now I want a user to click on ID Row and show a message in a label which row is clicked by a user. How can I do this? I've been googling for a long time.
You can find out the clicked row/column in the DataGridView.CellClick Event Handler.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// e.RowIndex
// e.ColumnIndex
}
Related
Is it possible to disable entire row selection when a cell clicked ?
i want to select row only if a checkbox column checked.
You could clear the selection this way:
private void DataGrid_SelectionChanged(Object sender, EventArgs e)
{
DataGrid.ClearSelection();
}
I can get the value of a clicked datagridView cell like this:
private void dgvCountryHits_CellClick(object sender, DataGridViewCellEventArgs e)
{
var cell = dgvCountryHits.CurrentCell;
MessageBox.Show(string.Format("{0}", cell.Value));
}
How do i allow clicking ONLY on Column[0]?
Ignoring clicks on the rest of the columns.
If you know which column it is you can use e.ColumnIndex == 1.
See this link: DataGridViewCellEventArgs
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
}
}
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)
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