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.
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.
I'm assigning textboxes to cells in a grid but will like to confirm if an object already exists in the cell before assigning. Is it possible to query a row at a specific column that returns null if empty?
I could create a list of lists representing the grid which I modify as i add and remove objects but this sounds to be inefficient.
A sample code I've written:
TextBlock _text = new TextBlock()
{
Text = _cont,
Background = new SolidColorBrush(_colo.disciplinecolor)
}; TextBlockStyle(_text);
int index = SearchDate((DateTime)_dt);
Grid.SetRow(_text, 1); Grid.SetColumn(_text, index);
Maindispgrid.Children.Add(_text);
Essentially this code block is called every time the user clicks a button with the TextBlock added to a dated column(pre-selected by the user), and hopefully, the next available row in the column . I've tried GetRow() but this searched by UIElement which didn't seem to work as all TextBlock are created with the same name.
I might have approached this all wrong so any leads as to what I need to read up on will be much appreciated.
Basically the end result should hopefully work as this:
TextBlock _text = new TextBlock()
{
Text = _cont,
Background = new SolidColorBrush(_colo.disciplinecolor)
}; TextBlockStyle(_text);
int index = SearchDate((DateTime)_dt);
//check for next available row at specific column index
Grid.SetRow(_text, nextAvailableRow); Grid.SetColumn(_text, index);
Maindispgrid.Children.Add(_text);
You can iterate over the children list via Maindispgrid.Children. For each child, retrieve the assigned Row and Column value (if assigned at all). Use this information to calculate the available row.
IList<int[]> indices = new List<int[]>();
foreach (var child in Maindispgrid.Children)
{
int rowIndex = Grid.GetRow(child);
int columnIndex = Grid.GetColumn(child);
indices.Add(new int[] {rowIndex, columnIndex});
}
// work with the "indices" list
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
{
}
}
I try to get data from the textboxex that is related to the ckeckboxex in the checkboxlist ..
in order to calculate the value of the selected box
this is my code :
foreach (ListItem item in listOthers.Items)
{
if (item.Selected)
{
sum += Convert.ToInt32(TextBox1.Text);//textbox of selected checkbox
}
}
lblSum.Text = sum.ToString();
What do I need to do to get the value of the textbox related to the checkbox?
Based on your comments, you have a CheckBoxList with a fixed size of 10 items and along with that 10 Textboxes (named TextBox1, TextBox2 and so on). The users marks some CheckBoxes and also enters a text into the corresponding Textboxes. You want to convert the Text of the Textboxes to an integer and calculate the sum of the marked entries.
You need to dynamically access the Textboxes by calling FindControl:
var index = 0;
foreach (ListItem item in listOthers.Items)
{
index++; // Increase here as the Textbox numbers start at 1
if (item.Selected)
{
var txt = FindControl("TextBox" + index.ToString()) as TextBox;
if (txt != null)
sum += Convert.ToInt32(txt.Text); //text of selected Checkbox
}
}
lblSum.Text = sum.ToString();
Please note that it is important to call FindControl on the container of the Textboxes. The call in the sample works if the Textboxes are located directly on the page. If they are located on a Panel named Panel1, you'd have to call:
var txt = Panel1.Findcontrol("TextBox" + index.ToString()) as Textbox;
An alternative to using a CheckBoxList that is prepared for variable sizes by design and having a fixed list of 10 TextBoxes next to it, would be to create a Repeater with a CheckBox and a TextBox in its ItemTemplate. Downsize is that you'd have to create some DTOs for DataBinding and also had to use some more dynamic control resolution (both for the CheckBox and the TextBox ).
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..