Gridview cell value and array list - asp.net

When I am tryng to pass the gridview cell values to an array everything is perfect but the array is empty after executing the code.
When I see through debug mode the index of selected row also is fine, i.e. when I select two rows using checkbox among 1000 the count shows exactly 2 in the array but the data is empty.
I'm not able to get the cell value from the gridview.
Any advice would be greatly appreciated.
protected void Button2008_Click(object sender, EventArgs e)
{
ArrayList checkedItems = new ArrayList();
foreach (GridViewRow row in this.GridView1.Rows)
{
if (((CheckBox)row.FindControl("cbRows")).Checked == true)
{
checkedItems.Add(row.Cells[12].Text);
}
}
Session["CheckedItems"] = checkedItems;
Response.Redirect("About.aspx", true);
}

you can use a breakpoint and check where the problem is occuring, for instance, does row.Cells[12].Text show any value ?
and you can check how your aspx page is acting after the postback.

Related

Telerik RadGrid GridDataItem - how to determine if column exists?

I'm using a base class to modify the behavior of any Telerik RadGrid that appears on my ASP.Net pages. In the base class, I want to perform certain operations (set Css, tool tips, etc) on many common columns, but not every common column exists in every grid.
In the ItemDataBound event I'm getting an instance to the GridDataItem and in turn I want to get a reference to one or more of the contained cells of the GridDataItem:
var cell = gridDataItem["ColumnUniqueName"]
Problem is that this throws a GridException if the named column doesn't exist:
Cannot find a cell bound to column name 'ColumnUniqueName'
Is there a way to test for existence of a column by name before referencing it or am I
stuck using try catch?
Will sent me on the right path:
var tableView = gridDataItem.OwnerTableView;
var gridColumn = tableView.Columns.FindByUniqueNameSafe(uniqueName);
if (gridColumn != null)
{
var cell = gridDataItem[gridColumn];
...
Try using the RenderColumns collection:
protected void rgGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
bool found = (from d in rgGrid.MasterTableView.RenderColumns select d).Any(d => d.UniqueName == "ColumnUniqueName");
}
}

Nested Foreach Loop not working?

I have 2 datagrids. Both with checkboxes. I have a nested foreach loop that gets the DataKeys values of both checked persons from both datagrids. When I run the debugger the first datakey gets assigned to the correct variable oIdividualID. That is working fine. The second foreach loop gets the datakey from the next datagrid. That works fine too. However, it is not assigning it to the variable oNewParentID. and At the bottom of my code, in debugging, the method I am calling holds the value of oIndividualID correctly, But oNewParentID Holds NO value? Is there something wrong with my nested foreach loop? I don't understand. Here's my code:
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow row in gvAdminCustomer.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkitemSelectorCustomers");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvAdminCustomer.DataKeys[row.RowIndex].Value));
foreach (GridViewRow r in gvReassignCustomers.Rows)
{
CheckBox chkBox = (CheckBox)row.FindControl("chkitemSelectorAllManagersandSalesman");
if (chkBox != null && chkBox.Checked)
{
int oNewParentID = Convert.ToInt32((gvReassignCustomers.DataKeys[r.RowIndex].Value));
Individual ind = new Individual();
ind.ReassignIndividual(oIndividualID, oNewParentID);
}
}
gvReassignCustomers.DataBind();
gvAdminCustomer.DataBind();
}
}
}
hard to understand the purpose of the code - for every checked tickbox in Admin grid you're running reassign on every checked Reassign grid. Is it really the intent?
And then you're running DataBind() for every line on in admin grid.
So.
Comment out DataBind() - you don't need them there, it's just tells the system to rebind the data to the grids which can potentially clear the user ticked tickboxes.
Not quite sure what happened, but suddenly it all started working. Problem solved but not sure how.

Two way binding with custom expression?

I am using a gridview in my asp.net project to view and modify some records from the database. The database has two columns: start_date and end_date. When a new record is created these columns contains null, but they can be modified later using the gridview update command.
In gridview I have two template fields (having names start_date and end_date) in which I have placed two calendar controls. Upon clicking an update link of gridview it always returns an error because of the null value binding to the calendar. I have used this helper function to solve it:
protected DateTime ReplaceNull(Object param)
{
if (param.Equals(DBNull.Value))
{
return DateTime.Now;
}
else
{
return Convert.ToDateTime(param);
}
}
and used these two custom expressions in calendar control's SelectedDate:
ReplaceNull(Eval("start_date"))
ReplaceNull(Eval("end_date"))
The problem is that two-way data binding the calendars upon selecting a date does not update the database table. Are there any workarounds? Or alternatively, a better solution would be appreciated.
I don't know why you let them null when insert a new record , but many ways you can solve this problem i think .
one of them : in the RowDataBound event of the Gridview
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1] == null) //the index of the start_date
{
e.Row.Cells[1].Text = DateTime.Now.ToString(); // in your case you will make the selected date of the calender(through casting to calender) with the value you need.
}
}
}
Or: you can catch the exception , you meet in your Update button through
try and catch block.

How can I retrieve a subset of data from entity object data source and pass to another page

I am playing about just now trying to teach myself a little bit about the entity framework.
I have a Gridview data bound to a Entity Date Source using the Entity Framework. If I select certain items in that list I then wish to redirect another page and populate another gridview with just the items selected (but with more detail, different includes/navigation properties)
This is probably the most simple thing but I have spent 2 hours banging my head on the wall trying to get this to work.
Essentially I have a continue button which when clicked should identify all the UIDs (a column in the gridview) of the rows and allow me to subset to just these rows and pass them to another page to be rebound to another datagrid
Any ideas???
Well, the big picture is that you should get those IDs, pass them to the other page, and then use a query with Contains; see this question for an idea of how to use it:
How search LINQ with many parametrs in one column?
Assuming you haven't used DataKeys in your GridView, this would be my approach.
Page 1
protected void Button1_Click(object sender, EventArgs e)
{
var checkedItems = new List<int>();
foreach (GridViewRow row in GridView1.Rows)
{
var checkbox = (CheckBox)row.FindControl("CheckBox1");
if (checkbox.Checked)
{
checkedItems.Add(int.Parse(row.Cells[1].Text));
}
}
Session["checkedItems"] = checkedItems;
Response.Redirect("Page2.aspx");
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
var checkedItems = (List<int>)Session["checkedItems"];
Session["checkedItems"] = null;
foreach (var checkedItem in checkedItems)
{
Response.Write(checkedItem);
}
}
Using the IDs in the checkedItems List you can now query those from you DB and finally assign the Result to your GridView on the second page.
Instead of using Session you could pass the IDs via QueryString.

Dropdownlist error when dynamically generated?

I am creating a dropdownlist called "ddlYears" dynamically like below code:
private void CreateDynamicDDL()
{
ddlYears.Items.Clear();
ddlYears.Items.Add(new ListItem("Year","0"));
for (int k = 0; k < 4; k++)
{
int time = int.Parse(DateTime.Now.Year.ToString());
ddlYears.Items.Add(new ListItem((time-k).ToString(),(k+1).ToString()));
}
}
and I have ddl selectedindexchanged event as below:
protected void ddlYears_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(ddlYears.SelectedValue.ToString());
Response.End();
}
In the above code I am trying to write the selected value. But it's working fine, if select the second item which is "2010" (I mean returning selected value as 1) and so on, but if I select first item which is "Year" it's not firing that ddlYears_SelectedIndexChanged event. Please somebody help me
Thanks
If you have "Year" as the first item then by default it is already selected. If you drop down the list and re-select it, it won't call the SelectedIndexChanged method.
Does it work if you select 2010 and then select Year again?

Resources