Load data from gridview into a textbox - c#

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.

Related

DevExpress: Row handle in gridview and returns a single row

I can’t to get the Row Handle in the GridView by the index in the DataTable DataSource.
I need to pass the value at this comand: gridView3.FocusedRowHandle = ????here????;
in short, what I'm doing. I have two datagrids, the first one must show the row highlighted and filtered, when I select information in the second datagrid.
Image by Form
in this example, I selected a row in the second datagrid that contains the value "cod.uniforme" = 15.
So the line was highlighted in red in the first datagrid, but the course was pointed in the 5th row
private void FirstDataGrid_RowStyle(object sender, RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
string CodUniforme = View.GetRowCellDisplayText(e.RowHandle, View.Columns["CODUNIFORME"]);
if (CodUniforme == txtValorGridView.Text)
{
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
e.HighPriority = true;
}
}
}

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

DGV ComboBox Cell remain updateable

I have a DataGridView that I want users to be able to add records to directly. This is done by clicking a link beneath the DGV, at which point a new row is programmatically added, and the first visible cell is of ComboBoxCell type. Code excerpt for how I'm doing this is:
// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);
// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;
// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here
// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";
// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);
The DataSource contains some values with an ID of -1, which are categories that the user should not be able to select, and all other values have their valid database ID. This all works fine, except that if a user has selected a -1 row I am unable to keep the combo cell in edit more, as it simply stores the value in the cell and I can no longer activate the drop-down. I have added the following code to the _CellValueChanged event, but it has no effect:
private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];
// If type of cell is ComboBox then
if (cell.GetType() == typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;
if ((int)cellInfoType.Value == -1)
{
MessageBox.Show("Going back into edit mode...");
dgv.CurrentCell = cell;
dgv.BeginEdit(true);
}
else
{
MessageBox.Show(cellInfoType.Value.ToString());
}
}
}
I do get the "Going back into edit mode..." message here after moving onto another cell, but it doesn't then do what it says! Could anyone please explain why it won't go back into edit mode, or is there a way of preventing the value becoming locked as soon as it has been selected?
Many thanks!
There should be a "validating" event for you to intercept and cancel if the value is -1.

XtraGrid whole row is highlighted except the clicked cell

When I select a row in the following GridView, the cell that my mouse is resting on (in other words the cell that I left click on) to select a row, is not highlighted while the rest of the row's cells are all highlighted.
I would appreciate your help.
GridView myView = (GridView)oGrid.MainView;
myView.OptionsSelection.MultiSelect = true;
myView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
if (myView.RowCount > 0)
{
frmChangeMyStatus ff = new frmChangeMyStatus(ccfrms);
DialogResult dr = ff.ShowDialog();
if (dr == DialogResult.OK)
{
for (int i = 0; i < myView.SelectedRowsCount; i++)
{
row = myView.GetSelectedRows()[i];
//...........
}
}
}
If you want the focused cell to look like any other cell in the focused row, disable focused cell styling in view properties. You can do this in two different ways:
At runtime:
myView.OptionsSelection.EnableAppearanceFocusedCell = false;
At design-time: Invoke XtraGrid designer, select Views :: (your view) :: OptionsSelection :: Set EnableAppearanceFocusedCell to False.
If you have access to XtraGrid designer, you can check out the Appearance section if you need more complicated styling rules.
In addition to what Yuriy Guts said above about the focus cell appearance for the view, if the cell that is selected is editable, it will still not highlight that cell.
So, if the cell doesn't need to be editable, you can set OptionsColumn.AllowEdit = false for that column. Otherwise, if the user selects a row by clicking on a cell, you have to live with that appearance so that the user can tell which cell they are currently editing.

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