I Have a data grid which can be queried based on a comboBox choice .
My code (shown below) is meant to search through the datagrid and if it finds a row with a matching piece of text it is meant to move the datagrids selected index to the corresponding row.
for (int i = 0; i <= DashBoard_DataGrid.Columns.Count - 1; i++)
{
if (DashBoard_DataGrid.Rows[0].ToString().ToLower().Contains(comboBox9.Text.ToString().ToLower()))
{
value = dr.Cells[i].Value.ToString();
// return dr.Cells[i].RowIndex;
DashBoard_DataGrid.SelectedCells[i].RowIndex = dr.Cells[i].RowIndex;
}
}
However I am getting the following error
Error 7 Property or indexer 'System.Windows.Forms.DataGridViewCell.RowIndex' cannot be assigned to -- it is read only
Does anyone know how to fix this error ? searching online hasn't givin a solution
You are trying to change a SelectedCell's row index, which is read-only. If you are trying to change the selected row, you need to set the SelectedIndex for the DataGrid.
DashBoard_DataGrid.SelectedIndex = dr.Cells[i].RowIndex;
Also, try changing SelectedCells to SelectedRows.
try this
.
DashBoard_DataGrid.ClearSelection();
DashBoard_DataGrid.Rows[3].Selected = true;
or if you want to select a specific cell, then
DashBoard_DataGrid.ClearSelection();
DashBoard_DataGrid[0, i].Selected = true;
this will select the first column of the desired row..
Related
I'm reading an Excel spreadsheet using the microsoft.office.interop.excel library in C#.
I need to first determine that a cell in the sheet contains a Dropdown and then read the value of the Dropdown.
I'm running through the rows of the sheet and parsing values, but it's possible that a cell contains a Dropdown with a selection instead of a text value and I'm having problems trying to get the Dropdown from a cell in order to read it's selected value.
The pseudo code below is an example of what I'm trying:
for(int row = 1; row <= rowCount; row++)
{
for(int col = 1; col <= colCount; col++)
{
//Have the row and column number of the individual cell here
Range range = worksheet.Cells.Item[row,col]; //Get the thing in the cell?
Type type = range.GetType(); //Try to get a type so I can compare it against a Dropdown?
string menuVal = range.Value(); //Both this and range.Value2() is null
}
}
What I'd like to do is something like this fantasy code:
for( -row loop- )
{
for( -col loop- )
{
if (Current Cell contains a Dropdown)
{
Dropdown menu = Get the Dropdown in this cell;
string value = Get the 'Dropdown' value;
}
}
}
Can someone please help me out here? I haven't been able to find any documentation on how to GET the value - plenty on how to create the Dropdown itself though.
Edit: Changed 'Menu' to 'Dropdown'. Dropdown is the actual class that's being used in the code.
Solution in a nutshell: The Dropdown class from microsoft.office.interop should -NOT- be used if you care about trying to actually retrieve the value of the dropdown programmatically. This has been either deprecated or is just something Microsoft doesn't want you using.
Instead you should use the method that sets up the Dropdown validation by using a separate worksheet that contains the values for the dropdown menu. You can then read the Value of the Dropdown cell as though it's just text.
The goal is to copy the selected cell data out of a selected row.
I'm doing this by catching the CopyingRowClipBoardContent event inside my datagrid and redirecting it to this code:
var currentCell = e.ClipboardRowContent[VwrGrid.CurrentCell.Column.DisplayIndex];
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add(currentCell);
This works perfectly! the only issue, is that if some of the columns are hidden, DisplayIndex reads improperly.
So if we have Item 1, Item 2, and Item 3.
If all are showing and I selected item3 and copy it, I get the cell value in Item 3.
The problem is, If Item 2 is collapsed/not shown, then copying Item 3 will tell you you're trying to copy out of bounds. because it's counted displayIndex , 3 from the left, and only two were shown. so it's moved outside of the table
For WPF Datagrid, try this:
// The clipboard row works only for visible cells
// To obtain the data column use the columnIndex and then map that to the Columns collection
int columnIndex = dataGrid.CurrentCell.Column.DisplayIndex;
var column = dataGrid.Columns[columnIndex];
// Now get the needed column
var cellContent = e.ClipboardRowContent.Where(i => i.Column == column).First();
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add(cellContent);
For Winforms:
Use .Index instead. .DisplayIndex applies only to visible columns.
Because this is WPF and I can't simply use an index, I just iterated through the columns and counter the number of columns that had their visibility set to collapsed up to the column we were attempting to grab the displayindex for. Then subtracted that number from the displayIndex.
private void DataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
//because we need to use displayindex, we need to check how many collapsed columns there are before our column, and adjust our display index accordingly
int invisibleCols = 0;
foreach(DataGridColumn col in VwrGrid.Columns)
{
if (col.Visibility == Visibility.Collapsed)
invisibleCols++;
if (col.Header.ToString() == VwrGrid.CurrentCell.Column.Header.ToString()) break;
}
try
{
var currentCell = e.ClipboardRowContent[VwrGrid.CurrentCell.Column.DisplayIndex - invisibleCols];
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add(currentCell);
}
catch
{
}
}
The following code shows the way i created the drop downs programmatically. It works fine. But now I need to get value of a specific dropdown of a cell.
Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
Microsoft.Office.Interop.Excel.DropDown xlDropDown;
xlDropDowns = ((Microsoft.Office.Interop.Excel.DropDowns)(sheet.DropDowns(Type.Missing)));
xlDropDown = xlDropDowns.Add((double)rag.Left, (double)rag.Top, (double)rag.Width, double)rag.Height, true);
var DropDownList = {"aaaa","bbbb","cccc","dddd"};
int x = 0;
foreach (var item in DropDownList)
{
x++;
xlDropDown.AddItem(item);
}
This is how i tried to get the xlDropDown value. currentCell is the cell where i have the drop down
ColumnVal = currentCell.Text; // This didnt give any output
OR
var dd = (Microsoft.Office.Interop.Excel.DropDown)currentCell.DropDowns(Type.Missing);
I know the 2nd one is wrong, because the cell range and drop down are 2 different things. But I tried all the options, still couldnt find any solution. Someone please help me
More clearly, I want to access a specific cell(currentCell), and the xldropdown it contains and then get value from it
First you would need a reference to the drop down you've just added:
*Assuming there's only one drop down, the below would do
xlDropDown = ((Excel.DropDown)(xlDropDowns.Item(1)));
then you need to access the .get_List() property of the Excel.DropDown while making sure that something has been selected.
Example:
if (xlDropDown.Value > 0)
{
sht.get_Range("A1").Value = xlDropDown.get_List(xlDropDown.Value);
}
else
{
throw new Exception("Nothing was selected yet");
}
Identifying the dropdowns:
You could a for each loop on the xlDropDowns collection and grab the .Name and .ListIndex of each xlDropDown?
foreach (Excel.DropDown xlDD in xlDropDowns)
{
MessageBox.Show(xlDD.Name + ", " + xlDD.ListIndex);
}
I am using mysql and asp.net with c#. I have a grid view which will display dynamically selected table data. I am able to display the data of selected table. In the first column i have added a check box and a Button outside Grid view. When user selects Check box and clicks on button, the selected rows must turn into text boxes. I am able to find the seleted check box, but i'm unable to convert the cells into text boxes. Here's my code:
int n = GridView1.HeaderRow.Cells.Count;
for( int i=0; i < GridView1.Rows.Count;i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
{
for( int j=0;j<n;j++)
{
TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
}
}
}
At this line: TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
i get a warning :
cannot convert 'System.Web.UI.Controls.TableCell' to type 'System.Web.UI.Controls.TextBox
I am unable to resolve this. Please help. Thank you
Try this.
You can remove one or few lines based on your hands-on with C#.
Concept is, you should create a TextBox, assign cell text that textbox and then Add that newly created textbox to child controls of Grid Cell of particular row.
Mark this solution if you found useful.
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if(isChecked)
{
for( int j=0;j<n;j++)
{
TextBox tbForCell = new TextBox();
tbForCell.Text = GridView1.Rows[i].Cells[j].Text;
GridView1.Rows[i].Cells[j].Text = "";
GridView1.Rows[i].Cells[j].Controls.Add(tbForCell);
}
}
If you want to avoid the TextBox to appear in CheckBox Column please initialise loop variable j with 1 instead of 0.
for( int j=1;j<n;j++)
Your method call is finding the cell. The Textbox is a control contained within the cell. Try something like this instead:
TextBox txt = ((TextBox) GridView1.Rows[i].Cells[j].FindControl("textbox name")).Text;
The FindControl method is documented here.
Also take #Bartdude's advice about using editable gridviews. If one will work at least as well as what you're trying to hand-roll, it's worth the time learning how to use it.
I have a table that I am displaying in a data grid view control. The user selects a single row from the control and presses a button. I need to retrieve the cells from that row and store them as strings.
Exactly how do I get the data using the SelectedRow method? I've been working on this for several hours and I'm at the end of my rope. Here's an example of something I've tried:
DataGridViewCellCollection selRowData = dataGridView1.SelectedRows[0].Cells;
If I try to access selRowData[x], the return value does not contain my data.
You're close - you need to reference each Cell through its index and return its Value property:
string firstCellValue = dataGridView1.SelectedRows[0].Cells[0].Value;
string secondCellValue = dataGridView1.SelectedRows[0].Cells[1].Value;
etc.
If you want the data and the data is likely bound to an datasource, then might I suggest that you get the key from the selection, and then you can use that to access the data any way you like:
dataGridView.SelectedDataKey.Value;
Try using the Item element of the dgv.
dgvFoo.Item(0, dgvFoo.CurrentRow.Index).Value
That would return the value of the first item. You could put that into a for loop to get them all.
Another option would be to use the SelectedRows collection on the object and iterate through each selected row (or just the one in your case).
Well there is no datagridview Item property..#Jay Riggs solution is better...Following solution also works:
string firstCellValue = dataGridView1[0,dataGridView1.CurrentRow.Index].Value.ToString();
string secondCellValue = dataGridView1[0,dataGridView1.CurrentRow.Index].Value.ToString();
Here 0 is the first column and dataGridView1.CurrentRow.Index is the current Row from where to get value.
Perhaps this is a more suitable solution use the cell values of the row clicked:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
var val = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
}