How to get cell index in DevExpress Gridview? - c#

I have 6 columns on gridview (shown as figure). How to get cell index? For example, when I handle double click Durum(Status) column or Detay(Detail) column on gridview, return index numbers. Actually I want to do that when I double click different cell of the row, happen different things. For example I double click the status cell of the row, change the status or I double click the detail cell of the same row, open new form. How can I do this?

Try this:
private void gridView1_DoubleClick(object sender, EventArgs e) {
var gv = sender as GridView;
var rowIndex = gv.FocusedRowHandle;
var columnIndex = gv.FocusedColumn.VisibleIndex;
}

Related

DataGridView row header click giving last selected columns index

I am using CellClick event to get the row and column clicked.
Now when the row header is clicked, it gives me the last column selected index.
This will create an issue for me since i need to perform actions on the current selected row and column. How can i tackle this? I have seen tons of help but all of these talk about column header click.
Here is my basic test to check the row and col index when cell is clicked
private void dgvUsers_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvUsers.CurrentRow == null || dgvUsers.IsLoading()) return;
var row = dgvUsers.CurrentCell.RowIndex; //CurrentRow.Index;
var col = dgvUsers.CurrentCell.ColumnIndex;
var ugactive = _ugActive;
}
In the above code, IsLoading is just an extension method that is checking for a particular text placed inside the tag.
In a DataGrdView CellClick() event, the current clicked cell can be determined inspecting the DataGridViewCellEventArgs e parameter, where e.RowIndex and e.ColumnIndex values report the reference indexes of the clicked cell Column and Row.
This also applies to Columns and Rows headers. In this case the value reported is negative (-1).
For example, if a Column header is clicked, e.ColumnIndex will report the index of the Column while the e.RowIndex will be -1. The opposite for a Row header.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//(...)
var row = e.RowIndex;
var col = e.ColumnIndex;
//(...)
}

Load data from gridview into a textbox

I want to load data from a row that when selected by means of a button loads them into a format. For this I am doing with the CommandName, as if they were coordinates, clicking on such a row will load this data and so on for all. But I do not know how to do it in rows and cells with textbox.
I did so like it's in the code but I do not know if it's fine.
Where you have two dashed lines is how I wrote it for the Textbox.
In the form (format) I have Textbox and there should automatically appear the data when selecting any row.
if (e.CommandName.ToString() == "clic") {
//DETERMINING THE INDEX OF THE SELECTED ROW
int indexrow = int.Parse(e.CommandArgument.ToString());
int id = (int)this.gridview.DataKeys[indexrow]["Id"];
-- TextBox1.Text = gridview.Rows[indexrow].Cells[2].ToString();
This is how you accomplish what you want (I think). But in your question I don't see a TextBox present in the GridView.
if (e.CommandName.ToString() == "clic")
{
//cast the container of the sender back to a gridviewrow
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
//get the row number
int indexrow = row.RowIndex;
//get the value of cell 2 in the textbox
TextBox1.Text = gridview.Rows[indexrow].Cells[2].Text;
}
If you mean to use "Edit" mode in the GridView with EditItemTemplate, then have a look at this tutorial.

Adding a single row with a single column of one datagridview into another datagridview of multiple column

I have defined a datagridview with 5 columns and another with 1 column ..what i want to do it is on the click of the button in the datagridview with the columns, the row with the single column of the other datagridview pops up..is it possible?..this is the screenshot of my datagridview..
i want to add a datarow of another datagridview with only one column..i know how to handle cellevent but what i really want is the addition of datarow with only one column..
Hope this helps:
private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
int newRow = dgv2.Rows.Add();
dgv2.Rows[newRow].Cells[0].Value = dgv1.Rows[e.RowIndex].Cells["Stringtext"].Value;
}
}
This should take the value from the StringText cell and place it in the first column of a newly created row in the second grid when the button in cell 4 is clicked in the first grid.
EDIT:
private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
dgv1.Rows[e.RowIndex].Cells["Stringtext"].Value = dgv2.Rows[{y}].Cells[0].Value;
}
}
This one should set the value of the cell "StringText"(which can be changed to whatever you need it to be) in dgv1 to the value of the cell in dgv2 with the rowIndex {y}.

c# DataGridView getting contents from a Row/Column

I have a DataGridView with 4 columns.
When a user double clicks the row, I want to revtrieve data from the 1st column of the clicked row.
private void ticketsDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Row " + e.RowIndex); // gets me the row selected
}
How can I get the column for the selected row?
Look at DataGridView.Rows Property
try this on doublic click event.
DataGridViewRow row =dataGridView.Rows[0];
string someStringColumnValue = (string)row.Cells[0].Value;
Ref: C# - Lambda syntax for looping over DataGridView.Rows

first locating, then changing the value of a cell in a selected row in a datagridview?

Im trying figure out how I can change the value of a specific cell in a selected row of a datagridview. I am able to change the value but not in the highlighted row, only the first one. The hghlighting of the row is defined in the "find" button code.
For example: I enter a text, it search each row for that text in the specific column and then scrolls to and highlights/selects the entire row. (This Works Fine)
I then want to be able to get the value of a specific cell of the highlighted row by selecting the column name already present in the dropdownlist(combobox). It then gets the value of that cell and deducts from it a value present in a numericalUpDownCounter(nudQTY).
So far i can do this for different cells selected by column name (cbSupplList) but it only does it for the first row not the highlighted row defined by the "Find" button Code.
The following is my attempt so far:
private void btnFind_Click(object sender, EventArgs e)
{
/* Code to search the alphanumneric Part Number (in Column1 header called "PART NUMBER")
and highlihgt the row*/
foreach (DataGridViewRow row in dataGridView1.Rows)
{
// Removes the row selection
row.Selected = false;
var cellValue = row.Cells["PART NUMBER"].Value;
if (cellValue != null && cellValue.ToString() == tbPartNum.Text.ToUpper())
{
// Scrolls the found row up and highlights the entire row
row.Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
}
}
}
private void btnSold_Click(object sender, EventArgs e)
{
int cellVal;
int newCellVal;
cellVal = Convert.ToInt32(dataGridView1.CurrentRow.Cells[cbSuppList.Text.ToString()].Value);
newCellVal = Convert.ToInt32(cellVal - nudQty.Value);
dataGridView1.CurrentRow.Cells[cbSuppList.Text.ToString()].Value = newCellVal;
}
It seems it may have to do with the SELECTED ROW in the "Find" code and the CURRENT ROW in the "Sold" Code. Making the SELECTED.ROW = CURRENT.ROW would solve it.... any ideas?
You are deselecting only Rows but no Cells.
dataGridView1.CurrentRow - "Gets the row containing current cell"
And Current Cell remains what it was = first cell of a first row.
In order to clear all selected rows/cells use
dataGridView1.ClearSelection()
And instead of
row.Selected = true;
use
row.Cells["PART NUMBER"].Selected = true;
As currently you are selecting only row but not the cell.
I hope this helps...
Otherwise you might want to find relevant cell in btnSold_Click() the same way you did in btnFind_Click()
.
.
EDIT
complete solution for you
private void btnSold_Click(object sender, EventArgs e)
{
int cellVal;
int newCellVal;
cellVal = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[cbSuppList.Text.ToString()].Value);
newCellVal = Convert.ToInt32(cellVal - nudQty.Value);
dataGridView1.SelectedRows[0].Cells[cbSuppList.Text.ToString()].Value = newCellVal;
}
I've just replaced CurrentRow with SelectedRows[0] and works fine.

Categories