Devexpress - Re-bind Chart Control error Index was out of range - devexpress

I has using dexexpress chartcontrol and bind the datasource in runtime.
chartControl1.DataSource = ds.Tables[0];
chartControl1.SeriesDataMember = "Task";
chartControl1.SeriesTemplate.ArgumentDataMember = "Resource";
chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Percentage" });
chartControl1.SeriesTemplate.View = new StackedBarSeriesView();
The first time binding, it has work fine and can show the chart.
When I click a button to re-create the dataset with new row of data, it give me an error in
chartControl1.DataSource = ds.Tables[0];
I has set the dataset = new dataset before fill it again with new data.
Any one has idea what wrong. Please help.

You should be able to do something along the lines of the following:
this.chartControl1.BeginInit();
DataTable chartData = this.chartControl1.DataSource as DataTable;
DataRow row = new DataRow()
{
"col1",
"col2"
}
chartData.Rows.Add(row);
this.chartControl1.RefreshData();
this.chartControl1.EndInit();
I hope this is helpful.

I have found that DevExpress occasionally gets confused when I set the DataSource, especially to an existing object. To work round this, before I set the DataSource to anything, I always set it to null first. Since doing that, I have not had any problems.
Give that a try.

Related

Binding database data to a dropdownlist

I have been trying for a long time to fix the bugs I'm encountering with my dropdownlists in my project. I'm getting data this with at the moment and it's working fine:
using (InfoEntities ie = new InfoEntities())
{
var sqlddl = (from query in ie.Table_Customers
from q in ie.Accounting_PriceType
where query.TypeID == 1 && q.ID == 1
orderby query.Customers
select query).Distinct().ToList();
DropDownListCust.DataTextField = "Customers";
DropDownListCust.DataValueField = "ID";
DropDownListCust.DataSource = sqlddl;
DropDownListCust.DataBind();
}
Now when the user saves the data and opens the website again I need the saved value that was chosen on the dropdownlist earlier retreived. This also works fine but the problem is I'm getting duplicates. Anyways I'm doing it like this and I'm pretty sure I'm doing it wrong:
On the page load i load my dropdownlist to get all the items plus the following to get the saved value:
DropDownListCust.SelectedItem.Text = sql.Customers;
This makes my DDL very buggy, some items dissapear and also sometimes dupicated values. Can anyone please help? I'm using LINQ but I can use some other methods as long as it's fixed.
Cheers
I solved my problem like this:
DropDownList1.ClearSelection();
DropDownList1.Items.FindByText(sql.Customer).Selected = true;

Dynamically creating ASP.NET form controls

I have a form which, based on the answers given in the prior page, can have about 10 different variations in the combination of fields (most are the same, but several change). I decided rather than making 10 separate pages, I would try to make it dynamic. Eventually this will pull the form setup from a database, but for now I'm just trying to get the dynamic part to work. The following code kinda works, but it's giving me a weird result.
private void AddTestControls()
{
var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);
var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);
newbox.ID = "SecondBox";
newbox.Text = "This is also dynamic";
newbox.CssClass = "step";
DynamicDiv1.Controls.Add(newbox);
}
I've stepped through it and all the properties are getting set correctly, but when the page finally renders, only the SecondBox control is visible. There is no trace of the FirstBox. If I change it so that SecondBox is its own object (newebox2 for example) then both are visible, but with how I was thinking that I would ultimately do the form from the database, this could complicate things. I don't understand why the textbox object has to be recreated in order to add it to the Div's collection of controls. Am I going about this all wrong, or just missing a step somewhere?
Your "SecondBox" are overwriting the "FirstBox" newbox since it's still holding a reference to it. Create a new TextBox for the second box:
var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);
var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);
// Create a new TextBox
var secondBox = new TextBox();
secondBox.ID = "SecondBox";
secondBox.Text = "This is also dynamic";
secondBox.CssClass = "step";
DynamicDiv1.Controls.Add(secondBox);
I'm not quite sure why this could complicate things, but what you could do is create a method for creating a textbox, if that's easier:
TextBox CreateTextBox(string id, string text, string cssClass)
{
var box = new TextBox();
box.ID = id;
box.Text = text;
box.CssClass = cssClass;
return box;
}
And then
var newBox = CreateTextBox("FirstBox", "This is dynamic", "stepHeader");
DynamicDiv1.Controls.Add(newBox);
What's how it suppose to work. newbox1 is a reference so after the first time it's added to DynamicDiv1, it's there and if you change its Text, then the Text will be changed. You may find this SO useful. This SO demostrates the same issue you are having.

How to dynamically hide fields in a DetailsView (fields count is always 0)

I am using a DetailsView to show the details of a single row from a DataTable.
I do not know the column names at design time, so I have AutoGenerateRows = true in the markup.
DataView dv = myDataTable.AsDataView();
dv.RowFilter = string.Format("ResourceID = {0}", resourceId);
dvScoresBreakdown.DataSource = dv;
dvScoresBreakdown.DataBind();
There are about 4 columns in the DataView which I don't want the DetailsView to display - mainly ID columns.
I understand that I should access the Fields property of the DataView and set the relevant fields invisible:
dvScoresBreakdown.Fields[0].Visible = false;
dvScoresBreakdown.Fields[1].Visible = false;
However, the .Fields.Count is always zero. So I get an index out of bounds exception.
When I say "always zero", I mean it's zero right after the .DataBind(), and also in the OnDataBinding, OnDataBound, and OnPreRender events.
But, the DetailsView does render on the page and show everything - all the columns in the original DataView - so the dataview is binding!
What am I doing wrong?
I've just found out, the way to do it is to remove rows right after the .DataBind() method.
dvScoresBreakdown.DataSource = dv;
dvScoresBreakdown.DataBind();
dvScoresBreakdown.Rows[0].Visible = false;
dvScoresBreakdown.Rows[1].Visible = false;
Hope this can help someone else!
The Columns collection only stores the explicitly declared columns, so if you’re using autogenerated columns, the count will be zero.
If you’re using autogenerated column, after databind you could loop through the rows collection and make the appropriate cells invisible, like:
If dvScoresBreakdown is GridView
dvScoresBreakdown .DataBind();
if (dvScoresBreakdown .Columns.Count > 0)
dvScoresBreakdown .Columns[0].Visible = false;
else
{
dvScoresBreakdown .HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in dvScoresBreakdown .Rows)
{
gvr.Cells[0].Visible = false;
}
}
I think this will help you.

CheckEdit as XtraGrid Column

edit = gridView1.GridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit;
column = gridView1.Columns.Add();
column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
column.VisibleIndex = 0;
column.FieldName = "CheckMarkSelection";
column.Caption = "Mark";
column.OptionsColumn.ShowCaption = false;
column.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
edit.NullStyle = StyleIndeterminate.Unchecked;
column.ColumnEdit = edit;
I suppose I succeed in adding a checkedit column into gridview but i still can't check multiple rows and can't access edit's check state. Why? i ll be appreciated, because i have been struggling with this for 3 days
Use the designer of the gridview. Goto columns, select the column you would like to be a checkedit. Go to ColumnEdit and select a checkedit. Then you really should be able to check multiple rows for this editor. Multiselect (selecting multiple rows simultaneously) is in the OptionsBehavior I guess. When I'm at work (tomorrow) I can provide you with a sample. It can't be very difficult that's for sure.
The problem appears because the column is marked as unbound. In this case, the GridView generates the CustomUnboundColumnData event which can be used to provide data to this column and save it. I think you should handle this event to resolve the original problem.
In the GridDesigner, for CheckEdit, set "NullStyle" property to "Unchecked", and for the column that you are going to use for CheckEdit, set "FieldName" property to the name of your column in the datatable (I used "col1" for boolean values true,false). After you set everything at the GridDesigner, you have to declare a type for the columns. For example i used a code like this;
public DataTable datas = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
datas.Columns.Add("col1", typeof(bool));
datas.Columns.Add("col2", typeof(string));
datas.Columns.Add("col3");
gridControl.DataSource = datas;
}
datas.Rows.Add(False, "someValue", "");
datas.Rows.Add(False, "someValue", "");
datas.Rows.Add(True, "someValue", "");
datas.Rows.Add(False, "someValue", "");
Than it should work. I hope it is the solution of your problem. Thanks.

customizing the auto generated grid view in asp.net/c#

I'm creating a column through ItemTemplate in my gridview, how can i adjust it to be my last column, unfortunately .net is making this column to be the first in grid view i want it to be the last column. The rest of the columns are automatically created by this code.
I mean
gridview1.datasource = myArrayList
gridview1.databind()
Please help me
Thanks in anticipation
Don't use the auto generation feature (AutoGenerateColumns="false") and supply the columns in the <columns> collection of the grid, or use LINQ:
var list = new List<MyClass>
{
new MyClass { Name = "A", Value = 1, Key = 1 }
};
gridView1.datasource = list.Select(i => new
{
i.Key,
i.Name,
i.Value
});
THe first option will be better on performance.

Resources