column value in datagridview - c#

i use these following codes to get cell's value in datagridview.
these codes show me each cell has been clicked.
i want to use some codes just show me special column for example column with index 1 but these codes show me each which has been clicked. Imagine i just want to show column 1 with it's clicked cell. please help me to solve this
string str = dataGridView1.CurrentCell.Value.ToString();
Thanks in advance

So you're trying to get the data from the second column for every row?
int index = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCell cell = row.Cells[index];
// Do something.
}
Edit: Per your comment, you're trying to get the value of the second column when any cell in the row is clicked. Try this:
string str = dataGridView1[1, e.RowIndex].Value.ToString();

Related

Displaying original rowindex after filter in datagridview

Just want to ask on how to get the original row index of selected row in DataGridView after filter.
I have DataGridView with 2 columns :name and age. And I have a TextBox that serves as filter. Let's say I have 8 records and upon filtering it goes to only 4 records and upon clicking the last record, I get row index of 4, while I need to get the original index of this row and display it on MessageBox. How will I do it?
Thank you.
Original row index means the index of the DataRow in the DataTable which can be found by DataTable.Rows.IndexOf(row). So to find the original index of the row you can use the following code:
var r = ((DataRowView)BindingContext[dataGridView1.DataSource].Current).Row;
var index = r.Table.Rows.IndexOf(r);
In case you are interested to do that for all rows in the DataGridView, as also is mentioned by Taw in comments, you can look into the DataBoundItem of the DataGridViewRow:
var r = ((DataRowView)dgvRow.DataBoundItem).Row; // dgvRow is a row of the DataGridView
var index = r.Table.Rows.IndexOf(r);
From your comments you said that you need it to display current record selected so I will not directly answer How to get row index on filtered table but how to get current record selected.
So to simply get current selected record use this code:
//Use this one if your datagridview SelectionMode is not FullRowSelect
DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
string name = row.Cells["Name"].Value.ToString();
int age = Convert.ToInt32(row.Cells["Age"].Value);
//If your datagridview SelectionMode is FullRowSelect then use this
DataGridViewRow row = dataGridView1.SelectedRows[0];
string name = row.Cells["Name"].Value.ToString();
int age = Convert.ToInt32(row.Cells["Age"].Value);
Reza answered main part of your question but if for some reason it is not working you can use this since your primary key is your NAME
foreach(DataRow r in yourDataTable.Rows)
{
if(r["NAME"].ToString() == row.Cells["Name"].Value.ToString()) //This row.cells... is the one from above code
{
int originalRowIndexInDataTable = dt.Rows.IndexOf(r);
return;
}
}

How do I find the value for the selected row in datagrid in C#

I want to get value of selected value in Datagrid in C#.
Thanks in advance
int index = datagridview1.SelectedCells[0].RowIndex;
DataGridViewRow row1 = datagridview1.Rows[index];
string str = Convert.ToString(selectedRow.Cells[Column name].Value);
please be specific in asking questions. adding existing codes may help you better.
DataGridView1.Rows[rowIndex].Cells[colIndex].value might be what you need
I Found My Answer in my way
Do this Coding on Data Grid Cell Clicked Event
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
String id = row.Cells[0].Value.ToString();
Do this coding on Data Grid Cell Click Event row.Cells[0] it Contains the Column of which I want my Value.

How to get the first column value of a selected row in GridView

I have a GridView
How do I get a value of a column for a given row.
I tried the following but it only gives the row number instead of the value:
int selRowIndex = ((GridViewRow)(((System.Web.UI.WebControls.CheckBox)sender).Parent.Parent)).RowIndex;
you are looking for the contentnot the index
var cellText= ((GridViewRow)(((System.Web.UI.WebControls.CheckBox)sender).Parent.Parent)).Cells[0].Text;
and then convert it to int

Select only a text of cell in datagridview c#

in a datagridview I want to select only the text of the cell and not the whole cell
if I put
Datagridview.CurrentRow.Cells.[Datagridview.CurrentCell.ColumnIndex].Selected=true;
the entire cell is selected
is there a way to select only the text of the cell like a vb6
Datagridview.SelStart=0
Datagridview.SelLength=Len(Datagridview.Text)
thanks for help
BeginEdit will select everything in the cell but you can specify the start and length of selection by:
((TextBox)dgv.EditingControl).SelectionStart = 0;
((TextBox)dgv.EditingControl).SelectionLength = 3;
You can do that by calling BeginEdit() method for the DataGridView control. It will start edit mode for the selected cell. You can also set the EditMode of the DataGridView control:
//set selected cell as you showed in your question
datagridview1.CurrentRow.Cells.[Datagridview.CurrentCell.ColumnIndex].Selected=true;
//call BeginEdit with true argument which will select all text within the cell
dataGridView1.BeginEdit(true);
//optionally set the EditMode before you call BeginEdit
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
This work for me, after adding a new row i want to edit the 2nd column of the row being added.
at same time the value of the cell is being highlighted. Hope this helps"
DataGridViewCell cellQty = dgvBundle.Rows[dgvBundle.Rows.Count - 1].Cells[1];
dgvBundle.CurrentCell = cellQty;
dgvBundle.CurrentRow.Cells[1].Selected = true;
dgvBundle.BeginEdit(true);
To get the current cell programmatically:
string msg = String.Format("Row: {0}, Column: {1}",
dataGridView1.CurrentCell.RowIndex,
dataGridView1.CurrentCell.ColumnIndex);
MessageBox.Show(msg, "Current Cell");

Get Column Header in TableCellCollection in asp.net

I am using a website and i have a gridview in that website.... I want to delete a row in that gridview by using that selected row column values and Column headers... i got the column values by using TablecellCollection but i cant get that column value's Corresponding Column header.... how shall i get that Column header by using TableCellCollection....
Please anyOne Tell me the solution of this....
Thanks in Advance!
And My code is:
int Row = Convert.ToInt16(e.RowIndex);
TableCellCollection collection = GrdViewDetails.Rows[Row].Cells;
for (int i = 0; i < Collection.Cells.Count;i++)
{
strColumnValue = Collection.Cells[i].Text;
//strColumnName=?
}
GrdViewDetails.Columns[i].HeaderText

Categories

Resources