I'm trying get values from a GridView using the following code:
foreach (GridViewRow row in this.dgvEstudios.Rows)
{
var xy = row.Cells[1].Text;
}
Always get a an empty string ("") as the value returned from .Text, why does this happen? I have set EnableViewState to true
If there are controls in each cell you will need to get the value like this:
Label lblEmailAddress = GridView.Rows[e.CommandArgument].FindControl("lblEmailAddress");
string Email = lblEmailAddress.Text;
in that case it it the control in the cell that has the value not the cell iteslf.
The cell might have controls inside it (e.g. LiteralControl or an HyperLink). This is what you should be looking for.
row.Cells[1].Controls collection, you should look for.
it could depend on many things.. Where is this code fired in relation to when the GridView is populated (Databind() called)?
Without any context, its hard to say what else it could be.
Related
My grid looks like this.
Key Value
1 A
2 B
3 C
I have Value column read-only in my grid. "Value" column's columnedit is associated with memoexedit repository. Now what I want to do is on some condition like when key=2,
I want my value cell to be editable.
I tried making the whole column ReadOnly = false.
and then handled ShowingEditor to cancel edit on everything else than Key=2, but that prevents even opening up the editor for other values.
What I want is able to see the editor but it should be readonly for Others
and for Key=2, It should be editable.
Please help!
Try to handle the ShownEditor event as the following (semi-pseudo code):
var grid = sender as GridView;
if (grid.FocusedColumn.FieldName == "Value") {
var row = grid.GetRow(grid.FocusedRowHandle) as // your model;
// note that previous line should be different in case of for example a DataTable datasource
grid.ActiveEditor.Properties.ReadOnly = // your condition based on the current row object
}
This way you could refine the already opened editor with your needs.
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 gridview with several ItemTemplates. The first contains a checkbox the rest contain textboxes.
I then added dynamically some bound controls like this:
BoundField bdfPrivName = new BoundField();
clsUtilities.SetBoundFieldCenter(ref bdfPrivName, "PrivName", "Priv Name");
BoundField bdfDescription = new BoundField();
clsUtilities.SetBoundFieldLeft(ref bdfDescription, "PrivDesc", "Description");
BoundField bdfLive = new BoundField();
clsUtilities.SetBoundFieldCenter(ref bdfLive, "Live","Active?");
grdExisting.Columns.Add(bdfPrivName);
grdExisting.Columns.Add(bdfDescription);
grdExisting.Columns.Add(bdfLive);
I then use FindControl() to locate the checkbox and textboxes and perform my logic based the result
foreach (GridViewRow gvr in grdMissing.Rows)
{
mckbAny = (CheckBox)gvr.FindControl("ckbAdd");
mtxtApplyDate = (TextBox)gvr.FindControl("txtAddApplyDate");
mtxtDateToAdd = (TextBox)gvr.FindControl("txtAddDateToAdd");
mtxtDateToRemove = (TextBox)gvr.FindControl("txtAddDateToRemove");
}
etc.
This all worked fine. I then got a request to put the bound fields as the second, third and fourth columns, after the check box and before the textboxes. I found that this was easy to do by changing the Add’s to Inserts as follows:
grdExisting.Columns.Insert(1, bdfPrivName);
grdExisting.Columns.Insert(2, bdfDescription);
grdExisting.Columns.Insert(3, bdfLive);
It looked fine of the page, but the FindControl(), all of them fail to work.
Please suggest a solution or a workaround.
Thanks in advance.
It sounds like you have come across this bug:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104994&wa=wsignin1.0
It appears ViewState is not stored (or restored) when a BoundField is inserted into a GridView. So when you do FindControl it doesn't exist.
You could try adding them as you did before and finding some way of re-arranging the columns (I think this is possible).
I am not sure how it was working for you before, as controls don't belong in row - they are inside cells. Anyways, the issue is that FindControl is not recursive, it will not search entire control tree - only immediate children of the control you run it on. You need to implement your own recursive findcontrol, for example like so:
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control c in Root.Controls)
{
Control fc = FindControlRecursive(c, Id);
if (fc != null)
return fc;
}
return null;
}
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();
}
}
The combobox displays a blank field by default even though the combobox is populated with a number of values
ColumnSpeed.DataSource = speedList;
ColumnSpeed.ValueType = typeof(string);
I also tried the following, but it still displays the blank text.
foreach (DataGridViewRow row in myDataGridView.Rows)
{
DataGridViewComboBoxCell cell = row.Cells[ColumnSpeed.Index] as DataGridViewComboBoxCell;
if (cell != null)
{
cell.DataSource = speedList;
cell.Value = cell.Items[0].ToString();
}
}
It could be that the ValueMember you assigned to your DataGridView is different from the DisplayMember you assigned. If that's the case, you'll get a blank value, plus you'll get a DataGridError firing.
You should try:
foreach (DataGridViewRow row in dgMain.Rows){
DataGridViewComboBoxCell pkgBoxCell = row.Cells[ColumnSpeed.Index]
pkgBoxCell.Value = ((Package) pkgBoxCell.Items(0)).Id
}
I converted that from vb.net, so it may not compile. In place of the line where i set the value, do whatever steps are necessary to retrieve and set the correct ValueMember value. In my example, I am casting the item to a specific type and using it's id.
I believe the code you have written should work .. just want to know where are you calling the same. It should work if you call it in the databinding_complete event of the grid
Once you set all the DataSources, try calling the DataGridView.Refresh() method. This is usually required to display changes to the DataSources.