Gridview , on row update get the value from a cell - c#

I have a simple gridview.
3 Columns |
NAME AGE Birthday
On RowUpdated I want to get something like:
Gridview1.Column[2].Cell[2].Text
so I can get the value from the column 2. How can I do it?
Thank you.
pS: row updated & row updating are the same thing?

Here's the MSDN article with example code: GridView.RowUpdating Event
Row updated and updating are not the same. Updating happens before the GridView control updates the row and updated is after the data has been updated.
In your Gridview control you need to add OnRowUpdating="TaskGridView_RowUpdating"
Assuming the value is in a textbox in the Age column this is how you would store the value in a string:
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = TaskGridView.Rows[e.RowIndex];
String str = ((TextBox)(row.Cells[1].Controls[0])).Text;
}

If you have data directly in the GridView cell (no label or textbox, etc) then this worked for me
protected void GridView1_RowUpdating(object sender,
System.Web.UI.WebControls.GridViewUpdateEventArgs e )
{
int row;
int colidx =1;
string cellvalue;
GridViewRow row = Gridview1.rows[e.rowindex]
cellvalue = row.cells[colidx].text;
}

Try this:
Gridview1.Rows[Gridview1.EditIndex].Cells[2].Text.ToString();

Related

How to get cell index in DevExpress Gridview?

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;
}

foreach looping only displaying last value of gridview

I'm binding my gridview with multiple datakeys. I'm trying to let these datakey value to be displayed on the textbox when a particular row of gridview is being selected. Therefore i tried the following method below.
I managed to store my database value into a string based on the column number. I'm using a foreach so as to loop the entire gridview when a particular row is being selected. But i'm not very sure why does it only display the last value of my gridview ?
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GWCase.Rows)
{
string detail = GWCase.DataKeys[gvr.RowIndex].Values[0].ToString();
string propertydetail = GWCase.DataKeys[gvr.RowIndex].Values[1].ToString();
string suspectdetail = GWCase.DataKeys[gvr.RowIndex].Values[2].ToString();
tbdetails.Text = detail;
tbproperty.Text = propertydetail;
tbsuspect.Text = suspectdetail;
}
I cant really convert it into a proper for loop because i got the gridview row there as well. Basically, i'm trying to display out the different value of the different datakey based on the selected gridview row.
Well I think you want value from the selected row only. While looping you are not checking if the row is selected or not. That is why it shows only the stuff from last row. Try following:
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow selectedRow = GWCase.SelectedRow;
if(selectedRow == null) return;
/*GET VALUES FROM selectedRow AND DISPLAY THEM*/
}
Answer to your question: Basically, i'm trying to display out the different value of the different datakey based on the selected gridview row.
To get the values from the selected row, Use DataKeyNames property of GridView. Make sure you have a selectbutton present using AutoGenerateSelectButton="true" OR adding a button and set its CommandName Property to "Select".
Set DataKeyNames property to the Primary Key of your table. This can also contain more than one fields.
<asp:GridView ID="Employees" runat="server" DataSourceID="sourceEmployees"
DataKeyNames="EmployeeID">
In your selectedIndexChanged event, you can access the Value of this PrimaryKey column as well as other columns:
protected void gridEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
int index = gridEmployees.SelectedIndex;
//retrieve the Primary Key field from the SelectedDataKey property.
int ID = (int)gridEmployees.SelectedDataKey.Values["EmployeeID"];
// Get other columns values
string firstName = gridEmployees.SelectedRow.Cells[2].Text;
string lastName = gridEmployees.SelectedRow.Cells[1].Text;
lblRegion.Text = gridEmployees.SelectedRow.Cells[9].Text;
}
One another way you can directly determine the data key value of the first key field of the selected row is by using the SelectedValue property
// Returns only the value of First DataKey Field
int ID = (int)gridEmployees.SelectedValue.ToString();
Managed to figure out the answer.
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = GWCase.SelectedIndex; //add this line in
string detail = GWCase.DataKeys[idx].Values[0].ToString();
string propertydetail = GWCase.DataKeys[idx].Values[1].ToString();
string suspectdetail = GWCase.DataKeys[idx].Values[2].ToString();
tbdetails.Text = detail;
tbproperty.Text = propertydetail;
tbsuspect.Text = suspectdetail;
}

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