I have a DataGridViewfilled with information from my SQL:
[Its only possible to click one complete row and not only a cell, shown in my picture]
I try some Code example from: Reading data from DataGridView in C# but it dosent work for my Problem.
I try this, because it seems good
dataGridView.Rows[MyIndex].Cells["MessageHeadline"].Value.ToString();
but i get an Error.
Now i want to take the Index (add with 1, because its start with 0) and if i press on a row, my program should take the information from my DataGridViewand give it back.
Try to use CurrentRow instead of SelectedRow. The selected row only perform if you have selected row from RowHeader or the RowSelection property is set FullRowSelect. But, CurrentRow is actually focused row. You can get value even you have selected only single cell.
dataGridView.CurrentRow.Cells["MessageHeadline"].Value.ToString()
Try this:
Set the SelectionMode property of your datagridview to CellSelect. Now you will be able to select a cell itself.
And in CellMouseClick Event:
private void MyGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
string FullContent = "";
for (int i = 0; i < MyGridView.Columns.Count; i++)
{
FullContent += MyGridView.Rows[e.RowIndex].Cells[i].Value.ToString() + "^";
}
FullContent = FullContent.Substring(0, Content.Length - 1);
string[] Content=FullContent.Split('^')
}
Now you can get each column content from Content array.
Like:
Content[0],Content[1],Content[2],etc.
For example, if you click the first row in your datagridview.
You can access the contents like:
FullContent; //1^Test BGW 1^Test^All^12.05.2014^.....
Splitted contents:
Content[0]; //1
Content[1]; //Test BGW 1
Content[2]; //Test
Content[3]; //All
Content[4]; //12.05.2014
just using the foreach loop can solve the problem.
foreach (DataGridViewRow row in gridStore.Rows)
{
MessageBox.Show(row.Cells[2].Value.ToString()); //row.Cell[index Here!]
}
Related
I have a DatagridView which contains row and data. I've added checkboxs to select one of the row (1) and then generate a PDF with the data of the selected row (2) (see picture) :
My code contains a part which check if checkbox is 1 or 0 and then I don't know how to get the data of the "checked row".. See
private void button_generer_pdf_Click(object sender, EventArgs e)
{
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGrid_factures.Rows)
{
if (Convert.ToBoolean(row.Cells[column_action.Name].Value) == true)
{
MessageBox.Show("OK!"); // Just to check if it undestands I've checked the row
//And then here I want to get highlighted data on the screenshot to create my Pdf
}
}
//PDF Generation here
The same way you got the data from your selection column "row.Cells[column_action.Name].Value" by changing it to be the right name so maybe
row.Cells["NOM"].Value
or you can use the array number should you know it eg
rows.Cells[3].Value
hi my friend you can use this code for any cell or use a loop for all cells!!!
public void ReadDataFromDataGridView()
{
string value = dataGridView1.SelectedRows[0].Cells["columnName"].Value.ToString();
}
I want to get a cell value particular focused row and previous row ?? I tried this code,
object obj1 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle - 1, gridView1.Columns["Each"]);
string str1= obj1.ToString();
textEdit1.Text = str1;
textEdit1.Text = gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Each"]);
but this code works for button but not work here in the RepositoryItemGridLookupEdit_ValueChanged Event or not work in the CustomUnboundDataEvent.
I want to get a cell value of gridview in Edit Value Change Event and then set in textEdit ? Help me.
At first this: textEdit1.Text = gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Each"]);
makes no sense, because you dont set the value of the TextEdit, just the value of the Cell. SetRowCellValue is void method!
To get the focused row and previous row, you have to handle the gridview.focusedrowchanged event. You can cast the row into value of your datasource and then assign the value to your textedit.
Example:
private void grvUebersicht_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
DataRow row = (DataRow)grvUebersicht.GetRow(e.FocusedRowHandle);
DataRow row2 = (DataRow) grvUebersicht.GetRow(e.PrevFocusedRowHandle);
TextEdit textedit = new TextEdit();
textedit.Text = row["MyColumn"].ToString();
}
Further you should think about using IList as DataSource. In my opinion it is more beautiful and modern style. If you need help iam ready to send example ;)
I cannot understand what is happening here:
void FindRecord(string pRecordID){
dgv_cusData.ClearSelection();
for(int i=0;i < dgv_cusData.Rows.Count;i++){
if (dgv_cusData.Rows[i].Cells["recordid"].Value.ToString()==pRecordID){
dgv_cusData.CurrentCell = dgv_cusData.Rows[i].Cells[1];
dgv_cusData.Rows[i].Selected = true;
}
}
}
pRecordID is a unique column in the DGV. I only have three records in the grid and I know that the selected record is in position 2; however then the SelectionChanged event fires:
void Dgv_cusDataSelectionChanged(object sender, EventArgs e)
{
MessageBox.Show(dgv_cusData.CurrentCell.RowIndex.ToString()); // <---- Returns 0;
lbl_DetailsLabel.Text = "Details For "+dgv_cusData.CurrentRow.Cells[1].Value;
}
The CurrentCell.RowIndex always returns 0. Am I missing something while trying to select a current row in the DGV? Any help would be appreciated. Cheers!
I wonder if selecting the row is was causes you to lose the CurrentCell.
Does the row highlight in the datagridview?
Do you allow multiple row selections on the grid. If not I think you would get better results with the following:
MessageBox.Show(dgv_cusData.SelectedRows[0].Index.ToString());
And:
lbl_DetailsLabel.Text = "Details For "+ dgv_cusData.Rows(dgv_cusData.SelectedRows(0).Index).Cells[1].Value;
I want to select row of previously selected rows after some event my code is as below.
int currentRow = dgvIcbSubsInfo.CurrentCell.RowIndex;
//code to execute
dgvIcbSubsInfo.Rows[currentRow].Selected = true;
after executing the code the preview will be as below. but i need to get the symbol > in id = 1272741 (blue selection) and not in 1272737
Probably you might have taken a look at the DataGridView.CurrentRow Property, which is a read-only property:
Gets the row containing the current cell.
But in the remarks section, there is written:
To change the current row, you must set the CurrentCell property to a
cell in the desired row.
Also, from the DataGridView.CurrentCell Property, we find out that:
When you change the value of this property, the SelectionChanged event
occurs before the CurrentCellChanged event. Any SelectionChanged event
handler accessing the CurrentCell property at this time will get its
previous value.
So, there is no need that you actually select the currentRow becasue it will be selected when you set the CurrentCell value (unless you have some code to be executed inside the current scope between the SelectionChanged and CurrentCellChanged events). Try this:
//dgvIcbSubsInfo.Rows[currentRow].Selected = true;
dgvIcbSubsInfo.CurrentCell = dgvIcbSubsInfo.Rows[currentRow].Cells[0];
I think you wish to highlight the row. Please try following code, I think it might help:
Color color = dgv.Rows[prevRowIndex].DefaultCellStyle.SelectionBackColor;
dgv.Rows[curRowIndex].DefaultCellStyle.SelectionBackColor = color;
Try the following to change the current row. Since the OP is a little unclear as to what row should be the new row, my example simply shows moving from the current row to the previous row (if there is a previous row). The first line of code is optional. You can also hardcode col to 0 (or some other column) to use a fixed column if you don't want to use FullRowSelect.
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int row = dataGridView.CurrentCell.RowIndex;
int firstRow = dataGridView.Rows.GetFirstRow(DataGridViewElementStates.None);
if (row != firstRow)
{
row--;
int col = dataGridView.CurrentCell.ColumnIndex;
dataGridView.CurrentCell = dataGridView[col, row];
}
I came here wanting to learn how to programmatically select rows in a DataGridView control. Here is how to select the top row in your DataGridView control named dg1 and "click" it:
dg1.Rows[0].Selected = true;
dg1_RowHeaderMouseClick(null, null);
This then calls the following event which is expecting one selected row.
private void dg1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
var selectedRows = dg1.SelectedRows;
// Make sure we have a single row selected
int count = selectedRows.Count;
if (count == 1)
{
tbAssemblyName.Text = dg1.SelectedRows[0].Cells[0].Value.ToString();
}
}
I had everything working when the user clicked the row they wanted. When there was only one record to choose from I wanted to "click" it for the user.
I have a form with a DataGridView (of 3 columns) and a Button. Every time the user clicks on a button, I want the get the values stored in the 1st column of that row.
Here is the code I have:
private void myButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in ProductsGrid.Rows)
{
if (this.ProductsGrid.SelectedRows.Count == 1)
{
// get information of 1st column from the row
string value = this.ProductsGrid.SelectedRows[0].Cells[0].ToString();
}
}
}
However when I click on myButton, the this.ProductsGrid.SelectedRows.Count is 0. Also, how do I ensure that the user selects only one row and not multiple rows?
Does this code look right?
Set DataGridView.MultiSelect=false and DataGridView.SelectionMode = FullRowSelect. This will make it so the user can only select a single row at a time.
SelectedRows only returns the rows if the entire row is selected (you can turn on RowSelect on the datagridview if you want). The better option is to go with SelectedCells
private void myButton_Click(object sender, EventArgs e)
{
var cell = this.ProductsGrid.SelectedCells[0];
var row = this.ProductsGrid.Rows[cell.RowIndex];
string value = row.Cells[0].Value.ToString();
}
Well, you don't need to both iterate over all rows in your grid and access the collection of SelectedRows. If you skip iteratating and use the SelectedRows collection, then your problem is probably an incorrect SelectionMode:
The SelectionMode property must be set
to FullRowSelect or RowHeaderSelect
for the SelectedRows property to be
populated with selected rows.
(from MSDN)
You can reference the grid similar to an array:
ProductsGrid[ProductsGrid.SelectedColumns[0].Index, ProductsGrid.SelectedRows[0].Index].Value;
By selecting the indexes from the first index of the SelectedRowsCollection and SelectedColumnsCollection you'll grab the first value if multiple rows are selected.
You can lock the user to selecting only a single row by setting the MultiSelect property on the DataGridView. Alternatively you make the CellClick event perform:
ProductsGrid.ClearSelection();
ProductsGrid.Rows[e.RowIndex].Selected = true;
SelectedRows.Count returns the number of entire rows that are currently selected. You probably want to use SelectedCells.Count.
you can also use the .BoundItem