CheckEdit as XtraGrid Column - devexpress

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.

Related

DevExpress Get Selected Cell DataGridView

I am having a bit of issues getting the ItemId of a selected Cell on my datagridview. The column ItemId is hidden from the user.
Any help will greatly appreciated. Thank you.
private void btnReturn_Click_1(object sender, EventArgs e)
{
Id = gridView1.GetFocusedDataRow()["ItemId"].ToString();
MessageBox.Show(Id);
}
Sample UI
Refer this - Obtaining and Setting Cell Values
It is possible to get the values of cells using the methods provided
by the grid's data source. For instance, the ColumnView.GetRow,
ColumnView.GetDataRow, ColumnView.GetFocusedRow and
ColumnView.GetFocusedDataRow methods return objects that
represent rows in a data source. After rows are obtained, use their
methods to retrieve field values.
For example, If your grid bind to a DataTable then you can get underlying data row for selected row in grid as below:
System.Data.DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
string cellValue = row[0].ToString();
If it is bind to some object data source then use GetRow method and cast it to your class object. Then you can access .ItemID property of that object.
MyClass row = gridView1.GetRow(gridView1.FocusedRowHandle) as MyClass;
if(row != null)
string id= row.ItemID;
Hope this reference help you..

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

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.

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");
}
}

Using Auto Increment ID with ASP.NET checkbox list

First and foremost, I'm pretty new to programming, so attention to detail is appreciated.
I've currently got an asp checkbox list that receives data from an SQL table. I'm encountering a problem where if there's 2 items that are exactly the same, my remove function will remove both items. The following is the code for that:
protected void btn_remove_Click(object sender, EventArgs e)
{
for (int i = 0; i < UPCList.Items.Count; i++)
{
if (UPCList.Items[i].Selected)
{
var item = UPCList.Items[i];
var frcID = item.Value;
_itemNumberRepository.Delete(frcID);
}
}
Response.Redirect("WebForm2.aspx");
Response.End();
}
public string Delete(string itemCode)
{
string connectionString = foobar
Int32 returnDeleted = 0;
string deleteUpcCode =
"DELETE FROM compare1 "
+ string.Format("WHERE itemcode = '{0}'",itemCode);
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(deleteUpcCode, connection);
command.Connection.Open();
returnDeleted = command.ExecuteNonQuery();
command.Connection.Close();
}
return returnDeleted.ToString();
}
I've been told to use Auto Incrementing ID from SQL so that each row will have a unique ID so I can delete only the selected lines. However, I don't even have a clue how to do that. I've already turned on the identity option in SQL for the itemcode column, but aside from that, I'm lost. How do I go about using that ID to delete only selected items from the checkbox list in asp?
When you say the items are "exactly the same", do you mean that they have the same item code? And item codes are allowed to be duplicated in your system (i.e. is that the correct business rule)?
If so, that's why you need a auto-generated ID for each line, so that each row is unique. Which means that you need to use that ID in your DELETE query instead of the item code.
Typically, in a (ASP.NET) web app, this sort of thing is done with a "grid". You can use a GridView with two columns: one is a checkbox and another column is just a label showing the item code. Each row is bound to the ID field.
So, I recommend that you start by checking out GridView (there's lots of examples out there on the web): MSDN documentation for GridView

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.

Resources