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

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;
//...
}

Related

Custom Cell Appearance in GridView change event

I am disabling a checkbox column in a XtraGrid GridView with the following code (works as expected). Got the code from this post https://www.devexpress.com/Support/Center/Question/Details/Q423605:
private void GridViewWeeklyPlan_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.FieldName == "Ignore")
{
CheckEditViewInfo viewInfo = ((GridCellInfo)e.Cell).ViewInfo as CheckEditViewInfo;
viewInfo.CheckInfo.State = DevExpress.Utils.Drawing.ObjectState.Disabled;
}
}
ISSUE
I want to enable the checkbox again when a certain column changes and has a value. This is where I am stuck and I thought I can change it in the GridView's CellValueChanged event, but I do not know how to reference the cell/column for the row:
private void GridViewWeeklyPlan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (e.Column.FieldName != "Reason") return;
if (String.IsNullOrEmpty(e.Value.ToString()))
{
//Make sure the checkbox is disabled again
}
else
{
//Enable the checkbox to allow user to select it
}
}
You need to refresh a cell in the Ignore column. You can do this by calling the GridView.RefreshRowCell method. To identify a row that you need to refresh, the CellValueChanged event provides the e.RowHandle parameter.
private void GridViewWeeklyPlan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (e.Column.FieldName != "Reason") return;
GridView view = (GridView)sender;
view.RefreshRowCell(e.RowHandle, view.Columns["Ignore"]);
}
The CustomDrawCell event will be raised again to update the cell appearance.

Unable to load Controls from Empty Data Template in GridView 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();
}

problem with gridview.selectedvalue

i have wrote a simple website with asp and have girdview in it.i want when i click on select in gridview know the index of row(primary key) so i use from girdview.selectedvalue like this in
page load
if (str == "book")
{
//do some works
if(this.IsPostBack)
if (GridView37.SelectedValue != null)
{
Session["Download"] = GridView37.SelectedValue;
}
}
but my problem is that GridView37.SelectedValue is null forever.what is problem?
What I do is have a method that is called by the OnRowCommand event and within the method do:
public void MyMethod(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
...
}
That gives the index of the row.

GridView doesn't refresh LINQ to XML data?

If have a Xml-file with 3 entries
Country Name="Denmark" InternetTLD="dk"
Country Name="Holland" InternetTLD="nl"
Country Name="Greate britain" InternetTLD="uk"
In my web-page if have a textbox, gridview and xmlDataSource.
When I don't specify anything in the textbox then all three countries are are loaded using a LINQ query into the xmlDataSource and then displayed in the gridview.
But when I specify for example 'Denmark' in the textbox then I keep on seeing all 3 records whereas the count property of the LINQ query says 1 and the Data-property of the xmlDataSource shows only 1 country as well (Denmark) as it should be.
Problem is that the gridView doesn't seem to refresh the new data???
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
QueryXml();
} // Page_Load()
private void QueryXml()
{
XElement _countries = XElement.Load(Server.MapPath(COUNTRIES_XML));
IEnumerable<XElement> query = null;
// extract all countries
if (txtCountry.Text == "" || txtCountry.Text == "*")
{
query = from c in _countries.Elements()
select c;
}
else
{
// Extract all elements where Name has been specified
query = from c in _countries.Elements()
where c.Attribute("Name").Value.ToString().StartsWith(txtCountry.Text, StringComparison.CurrentCultureIgnoreCase)
select c;
}
// Set xml filtered string data source
xmlDsCountries.Data =
"<Countries>" +
string.Join("", query.Select(country => country.ToString())) +
"</Countries>";
// set the data source of the gridview
gdvwCountries.DataSource = xmlDsCountries;
// Show the data
gdvwCountries.DataBind();
lblMsg.Text = query.Count().ToString();
} // QueryXml()
Why isn't the gridView refreshing?
thank you
Chris
Try turning off caching on the XmlDataSource
protected void Page_Load(object sender, EventArgs e)
{
xmlDsCountries.EnableCaching = false;
//...........the rest.............
}

Read data from SqlDataSource or GridView

I have SqlDataSource and GridView on web form. GridView.DataSourceID = mySqlDataSource.
When I call myGridView.DataBind(), all data successfully bind on the page.
How is it possible to read already got data from mySqlDataSource or myGridView objects as DataTable or DataView? Thanks
The data in the gridview can read by using FindControl property of Gridview control. For example, for reading values set in the Checkbox column in the grid.
for (i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("checkbox1");
//here code for using value captured in chk
}
Provided your data set isn't gigantic, you could store it in the Session, bind your GridView to the Session data, and then re-use the Session data in your other web objects.
Your code would then look something like this (you'll have to forgive any minor inaccuracies -- I'm away from my development box at the moment):
protected override OnInit(object sender, EventArgs e)
{
base.OnInit();
if (Page.IsPostback == false)
{
SetSessionData();
}
//
// Set your GridView, DataTable, DataView, etc. events here.
//
}
void SetSessionData();
{
List<YourDataBoundObject> myDataBoundObject = GetYourDataBoundObject(); // Or Collection<T>, IEnumerable<T>, etc.
Session["data"] = myDataBoundObject;
}
void YourGridView_Load(object sender, EventArgs e)
{
BindYourGridView();
}
void BindYourGridView()
{
YourGridView.DataSource = GetSessionData();
YourGridView.DataBind();
}
List<YourDataBoundObject> GetSessionData()
{
return (List<YourDataBoundObject>) Session["data"];
}
void YourDataTable_Load(object sender, EventArgs e)
{
BindYourDataTable();
}
void BindYourDataTable()
{
YourDataTable.DataSource = GetSessionData();
YourDataTable.DataBind();
}

Resources