I've not really used the MPE before & currently have a need to, what I'm slightly confused about is losing reference to the initial control that triggers the function.
I have a gridview with a dropdownlist and onselectedindexchanged set to call a function when a value has been selected, when processed one value i grab is the RowID into a variable, out of 9 list items only one requires the pop up (Ok = proceed & Cancel = Return) & when this is triggered I lose all reference to the given grid row which is referenced via;
DropDownList ddlid = (DropDownList)sender;
GridViewRow gvrow = (GridViewRow)ddlid.NamingContainer;
RowID = (Int32.Parse(GridView1.DataKeys[gvrow.RowIndex].Values[0].ToString()));
Can anyone shed any light or have an idea on how I can keep track of the gridview row?
Thanks
IchBinDicky
Used session object in the code behind (point of loss) in the end as it fit a few other issues I'd encountered at the same time;
public static string TabIndxValue
{
get
{
object value = HttpContext.Current.Session["TabIndxValue"];
return value == null ? "" : (string)value;
}
set
{
HttpContext.Current.Session["TabIndxValue"] = value;
}
}
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 have a form where I have 4 dropdowns Country, State, District and City. Country is populated on form load and rest are populated on selected index changed event. The sequence is On Country's selected index changed States are populated. On State-> Districts populated and on Districts -> Cities are populated.
For saving it worked fine but when i was updating value, it shows null reference error. On debugging I got, the dropdowns are not populated, whereas I was trying set values. Below is my code.
using (var manager = new LocationManager())
{
var dt = manager.GetLocationById(i);
if (dt.Rows.Count == 1)
{
Countries.SelectedValue = Countries.Items.FindByText(dt.Rows[0]["Country"].ToString()).Value;
BindStates(Convert.ToInt32(Countries.SelectedItem.Value));
States.SelectedValue = States.Items.FindByText(dt.Rows[0]["State"].ToString()).Value;
BindDistricts(Convert.ToInt32(States.SelectedItem.Value));
Districts.SelectedValue = Districts.Items.FindByText(dt.Rows[0]["District"].ToString()).Value;
BindCities(Convert.ToInt32(Districts.SelectedItem.Value));
Cities.SelectedValue = Cities.Items.FindByText(dt.Rows[0]["City"].ToString()).Value;
Pincode.Text = dt.Rows[0]["Pincode"].ToString();
ViewState["Id"] = dt.Rows[0]["LocationId"].ToString();
}
}
I am getting error: {"Object reference not set to an instance of an object."} at method
BindStates(Convert.ToInt32(Countries.SelectedItem.Value));
I think the problem is not with the population of drop downs, but with finding values, when
Countries.Items.FindByText(dt.Rows[0]["Country"].ToString())
wont find any matching value it will return null. So applying 'Value' property on null is giving the error.
you can put a null check before selecting it.
something like below:
countryDropDown.SelectedValue = countryDropDown.Items.FindByText(dt.Rows[0]["Country"].ToString()) != null ? countryDropDown.Items.FindByText(dt.Rows[0]["Country"].ToString()).Value : countryDropDown.Items.FindByText("-Please select-").Value;
Check if Countries.SelectedItem != null
#Amit Ranjan: Bind all values in page init method and at page load make the perticular value selected for all drop downs:
you are getting null reference because, there is no value already bind to the drop down.
At page init bind all values to dropdown
India
US
UK
asp:DropDownList>
At page load
DropDownList .SelectedValue= dt.Rows[0]["Country"].ToString()).Value;
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.
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.