Unable to load Controls from Empty Data Template in GridView asp.net - asp.net

I have a GridView which is binded with DataTable for the datasource, when there is no records in the underlying datasource i show up the EmptyDataTemplate with Two DropDown (State and City) and based on the State i fills up City.
Now my problem is till i have some records inside the Datasource it works fine, but as soon as i delete the last remaining record from the datasource, it throws error, it switches to the emptydatatemplate but it could not find the States Dropdownlist (based on which i am filling city dropdown). But once i refresh the page it works fine.
I don't understand which event to grab to place the codes
Here's what i am doing
Below function gets the stateid and fills the cities based on it
private void FillCitiesByStateId()
{
drpCity = grdLocationView.Controls[0].Controls[0].FindControl("drpCitiesAdd") as DropDownList;
drpState = grdLocationView.Controls[0].Controls[0].FindControl("drpStatesAdd") as DropDownList; // Fetches the States DropDown from EmptyDataTemplate.
if (drpState != null)
{
objCity.StateId = Convert.ToInt32(drpState.SelectedValue); //It always throws error on this line.
drpCity.DataSource = objCity.GetCitiesByStateId();
drpCity.DataTextField = "Name";
drpCity.DataValueField = "Id";
drpCity.DataBind();
}
}
Below is the code i which is used to bind the records in dropdowns
protected void grdLocationView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (dt.Rows.Count == 0)
{
FillCitiesByStateId();
}
else
{
if (e.Row.RowType == DataControlRowType.Header)
{
drpCity = e.Row.FindControl("drpCities") as DropDownList;
drpState = e.Row.FindControl("drpStates") as DropDownList;
objCity.StateId = Convert.ToInt32(drpState.SelectedValue);
drpCity.DataSource = objCity.GetCitiesByStateId();
drpCity.DataTextField = "Name";
drpCity.DataValueField = "Id";
drpCity.DataBind();
}
}
}
Can anyone provide any suggestion whats going wrong with it

Make sure you are only calling the FillCitiesByStateId() for appropriate row types. Maybe you're calling it for a footer row?
DataControlRowType Enumeration
instead of:
if (dt.Rows.Count == 0)
{
FillCitiesByStateId();
}
try:
if (e.Row.RowType == DataControlRowType.EmptyDataRow)
{
FillCitiesByStateId();
}

Related

devexpress - winforms - xtragrid - filtering on unbound column data throws NullReference exception

I have an xtragrid that I populate and has 2 unbound columns. Everything works fine except when i go to filter the columns, the popup of filtering displays correctly but when i double click on a value i get a nullreference exception. I managed to find where the exception is and it is in my method CustomColumnUnboundData where i try to get the current object from the current row and the object is null. Can you help me? What am I doing wrong ? e.ListSourceRowIndex or GetRow() does not seem to work on filtering...
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
GridView gridView = (GridView)sender;
int dataSourceIndex = e.ListSourceRowIndex;
Person person = (Person)gridView.GetRow(dataSourceIndex);
if (e.Column.FieldName == "name" && e.IsGetData)
{
e.Value = person.PersonKey.Name;
}
if (e.Column.FieldName == "surname" && e.IsGetData)
{
e.Value = person.PersonKey.Surname;
}
}
Solved.
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
GridView gridView = (GridView)sender;
Person person = e.Row as Person;
//...
}

Gridview paging Show all Records

i have a gridview that has paging enabled. i also have a dropdown on the pager of the gridview where the user can select how many records per page they would like to retrieve. Once the dropdown is changed then an event is fired (shown below) to rerun the query with the updated results per page request. This works very well. I did however want to have a value "All" on the dropdown aswell and the method i used to impliment this is by disabling paging.
This all works brilliantly except for one issue. When the user selects "All" on the dropdown i would like to still show the pager once the gridview is updated. It doesnt show because i turned off paging but is there a way to show the pager again? See my code below for the event. (As you can see i renable the pager at the end but this has no effect)
thanks
damo
Code behind Event for Dropdown Change
void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e)
{
//changes page size
if ((((DropDownList)sender).SelectedValue).ToString() == "All")
{
GridViewMain.AllowPaging = false;
}
else
{
GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
}
//binds data source
Result fAuditOverallStatusLatest = new Result(sConn);
GridViewMain.DataSource = Result.getAuditOverallStatusLatest();
GridViewMain.PageIndex = 0;
GridViewMain.DataBind();
GridViewMain.AllowPaging = true;
GridViewMain.BottomPagerRow.Visible = true;
GridViewMain.TopPagerRow.Visible = true;
}
DDL Code behind
protected void GridViewMain_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList GridViewMainddl = new DropDownList();
//adds variants of pager size
GridViewMainddl.Items.Add("5");
GridViewMainddl.Items.Add("10");
GridViewMainddl.Items.Add("20");
GridViewMainddl.Items.Add("50");
GridViewMainddl.Items.Add("100");
GridViewMainddl.Items.Add("200");
GridViewMainddl.Items.Add("500");
GridViewMainddl.Items.Add("All");
GridViewMainddl.AutoPostBack = true;
//selects item due to the GridView current page size
ListItem li = GridViewMainddl.Items.FindByText(GridViewMain.PageSize.ToString());
if (li != null)
GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li);
GridViewMainddl.SelectedIndexChanged += new EventHandler(GridViewMainddl_SelectedIndexChanged);
//adds dropdownlist in the additional cell to the pager table
Table pagerTable = e.Row.Cells[0].Controls[0] as Table;
TableCell cell = new TableCell();
cell.Style["padding-left"] = "15px";
cell.Controls.Add(new LiteralControl("Page Size:"));
cell.Controls.Add(GridViewMainddl);
pagerTable.Rows[0].Cells.Add(cell);
//add current Page of total page count
TableCell cellPageNumber = new TableCell();
cellPageNumber.Style["padding-left"] = "15px";
cellPageNumber.Controls.Add(new LiteralControl("Page " + (GridViewMain.PageIndex + 1) + " of " + GridViewMain.PageCount));
pagerTable.Rows[0].Cells.Add(cellPageNumber);
}
}
Put this in your Page_Init:
GridViewMain.PreRender += new EventHandler(GridViewMain_PreRender);
Then elsewhere in your Page class:
void GridViewMain_PreRender(object sender, EventArgs e)
{
var pagerRow = (sender as GridView).BottomPagerRow;
if (pagerRow != null)
{
pagerRow.Visible = true;
}
}
Then for your drop down event:
void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e)
{
MyServices fServices = new FAServices(sConn);
Result fAuditOverallStatusLatest = new Result(sConn);
var data = Result.getAuditOverallStatusLatest();
//changes page size
if ((((DropDownList)sender).SelectedValue).ToString() == "All")
{
GridViewMain.PageSize = data.Count();
}
else
{
GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
}
//binds data source
GridViewMain.DataSource = data;
GridViewMain.PageIndex = 0;
GridViewMain.DataBind();
GridViewMain.AllowPaging = true;
}
In that PreRender event, you'll have to duplicate that code for the top pager.
Edit: To get All selected, make this change in GridViewMain_RowCreated:
if (li != null)
{
GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li);
}
else
{
GridViewMainddl.SelectedIndex = GridViewMainddl.Items.Count - 1;
}

ASPxCombobox not fetched from SQLdatasource saved in session

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["SavedSQLSources"] == null)
SavedSQLSources = new Dictionary<String, SqlDataSource>();
SavedSQLSources.Add(ASPxComboBox1.ID, SqlDataSource1);
SavedSQLSources.Add(ASPxComboBox2.ID, SqlDataSource2);
Session["SavedSQLSources"] = SavedSQLSources;
}
else
{
if (Session["SavedSQLSources"] != null)
SavedSQLSources = (Dictionary<String, SqlDataSource>)Session["SavedSQLSources"];
}
}
Greetings, I have multiple ASPxCombobox with each having their own datasource. So first I saved
each control ID with their corresponding datasource object in a dictionary.
protected void Cmb_Callback(object source, CallbackEventArgsBase e)
{
ASPxComboBox comboBox = (ASPxComboBox)source;
string[] args = e.Parameter.Split('|');
for (int i = 0; i < args.Length; ++i)
SavedSQLSources[comboBox.ID].SelectParameters[i].DefaultValue = args[i];
comboBox.DataSourceID = SavedSQLSources[comboBox.ID].ID;
comboBox.DataBind();
}
Doing a few actions on the page, each control then launch its callback and bind its data with the corresponding datasource.
Well... Work perfectly when using directly the datasource, but having no items fetched when it's from a datasource saved in Session (from SavedSQLSources).
Shouldn't the instance of the object be the same ?
Thanks in advance, TheRainFall.
Ok, I gave up on the dictionnary method and resolved this by linking each ASPxCombobox to its SqlDatasource from client-side:
DataSourceID="SqlDataSource1"
Then in the callback I got the sqldatasource instance from the parent container by using the datasourceID referenced from client-side:
SqlDataSource tempSqlDatasource= (SqlDataSource)comboBox.Parent.FindControl(comboBox.DataSourceID);
The main purpose was to reload all combobox without reloading the page although I could have done this client-side only.

Dropdownlist in repeater control

I have 3 dropdownlist which will have set of values from the database. In my page, I have different controls. I was planning to add this dropdownlist in a repeater control.
When a user selects a value, the value will be saved to the database, either by a save button inside the control or automatically.
Could you please let me know if this is possible? If yes, any code that can be shared would be helpful.
Yes, it's possible. The trick is that the DataSource for the dropdownlists are separate than the DataSource of the Repeater.
Here's the sample code:
protected void cmdSave_Click(object sender, EventArgs e)
{
foreach (RepeaterItem ri in GeneralRepeater.Items)
{
switch (ri.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
DropDownList GetValue = (DropDownList)ri.FindControl("GeneralDDL");
var sSelectedValue = GetValue.SelectedValue;
for (int index = 0; index <= PocDiagnoses.MAX_DIAGNOSIS; index++)
{
foreach (RepeaterItem ri1 in GeneralRepeater.Items)
{
int iItemIndex = ri1.ItemIndex;
DropDownList myDDL = (DropDownList)GeneralRepeater.Items[index].FindControl("GeneralDDL");
FirstPlanOfCare.Diagnoses.Diagnoses[index] = new PatientDiagnosis(myDDL.SelectedValue, new SynergyOnSetDate(new System.DateTime(Year, Month, Day)), "01/02/2011"); //Insert Diagnosis Value
}
}
break;
}
}
//Create
Chart.AddPlanOfCare(FirstPlanOfCare);
}

gridview column delete

Is there any other way to delete gridview column except using index..
I am using gridview with multiple checkbox.
I want to delete all those column whose checkboxes are checked..
I am retriving checkboxes as..
protected void ButtonApprove_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
//find the CheckBox
CheckBox cb =
(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb != null)
{
if (cb.Checked)
{
// get the id of the field to be deleted
id = GridView1.Rows[i].Cells[1].Text;
// add the id to be deleted in the StringCollection
sc.Add(id);
}
}
}
UpdateRecords(sc);
}
Please go through the Link for deleting multiple items
Check this to move the selected to another gridivew Move

Resources