DevExpress Get Selected Cell DataGridView - devexpress

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..

Related

How to use "strongly typed data table" (or "row") in WebForms code-behind?

Our asp.net WebForms application is using table-adapters for "strongly typed data tables" and data-rows. In the code-behind I find I cannot use the return value directly from the BLL-class instance (such as our "Main_TblAdap.CostDataTable") as in referring to the strongly-typed data-rows and columns/fields within.
What is the proper way to make use of the strongly-typed data-table or data-row within the code behind events and methods?
Further, we want the ability to reference a specific data-row or sort/filter the data-table.
A good coding example would be very helpful showing the best way to (1) get from a strongly-typed data-table to reference values in a specific data-row from within the data-table and (2) how to sort/filter the strongly-typed data-table.
First create a DataView from the Table. You can either customize the view on initialization or call get default.
DataView dv = yourTable.DefaultView;
Or go ahead and filter the view from the table when you create your reference pointer like so:
DataView custDV = new DataView(YourTable[yourTableName],
"VehicleID = 'xxx'", // Row filter
"VehicleID", // Sort
DataViewRowState.CurrentRows);
Now you can sort the View and filter by Row state
dv.Sort();
view.RowStateFilter = DataViewRowState.Added |
DataViewRowState.ModifiedCurrent;
ROWS.
You can interact with the DataRowView IEnumerable within the DataView.
foreach (DataRowView rowView in thisDataView)
{
// your code here
}
OR
foreach(DataRow r in dt.Rows)
{
// your code here
}
To find rows in the table try:
var result = dt.AsEnumerable().Select(r => r["Id"] = 4);
if(result !=null)
{
int x = 0;
}
Now just bind to your view and if you add a row or update it, the view gets updated as well.

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

How to display the master-detail tables into two xtragrid?

I have to tables:users and messages.And create the one-many relations between these tables.And,If I display this two table in single xtragrid, it's no problem,But ,I want to display into two grid,the detail of messages doesn't display. please help me!
the key code is :
1.create the dataset of users and messages:
DataSet ds = new DataSet();
ds = SqlHelper.ExecuteDataset(fbh.ConnectionString, CommandType.Text, s);
DataTable mess = ds.Tables[0];
mess.TableName = "Messages";
DataTable user = deptmentUserMessages.Tables["Users"];
DataTable m = new DataTable("Messages");
m.Merge(mess);
deptmentUserMessages.Tables.Add(m);
deptmentUserMessages.Relations.Add("**UserMessages**", user.Columns["UserName"], m.Columns["SENDER"]);
the deptmentUserMessages is the static dataset
2.in the form.load event,I have to do:
gcMessage.DataSource = master;
gcMessageDetail.DataSource = detail;
master.DataSource = pub.DeptmentUserMessages;
master.DataMember = "Users";
detail.DataSource = master;
detail.DataMember = "**UserMessages**";
the master and detail are BindingSource.
You should handle xtargrid FocusedRowChanged event to populate detail grid.
Get the primary key field value to fetch values from child table and then set the data source of child grid. Use GetRowCellValue(Int32,String) Method
private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) {
if(e.FocusedRowHandle >=0)
{
/// Get master table selected row's primary column value
/// then create a DataView from the detail table
// and set datasource of detail grid.
dvUserMessages = (Filtered row by primary column value);
master.DataSource = dvUserMessages.ToTable();
}
}
Reference:Parent - Child relationship between Two GridViews
Using only event FocusedRowChanged is not enough. When the DataSource is changed, this event doesn't fires. Because the position property doesn't changes, it remains 0. And it doesn't fires when user filters rows in master grid. So, add to this example similar event handler on ColumnFilterChanged event, and also populate child grid just after setting DataSource property of master grid.

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.

Retain the value entered in the cell of DevExpress Xtragrid

I am using DevExpress Xtragrid control in my C#.net windows application.
I enter some value into the first cell of the grid and if i go to second cell , the value entered in the first cell disappears.
How to retain the value entered in the cell ?
I am assuming that you are using this for an unbound column in a gridView (Xtragrid), first step is make sure to go to the column properties, and change the UnboundType property value to the datatype that you will be entering into that column, example below uses double.
Assign the CustomUnboundColumnData event to your gridView. Make sure that you declare a class level variable (named _userEnteredData in code sample below) to hold the value that you are entering into your gridView, then add the following piece of code, but make sure that you change the names to match your gridView and variable names:
Class level variable declaration:
private double _userEnteredData = 0;
Now the event:
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column == gridColumn_YourColumn && e.IsSetData)
{
_userEnteredData = Convert.ToDouble(e.Value);
}
else if (e.Column == gridColumn_YourColumn && e.IsGetData)
{
e.Value = _userEnteredData;
}
}
I hope this helps.
You can get further details from here:
http://documentation.devexpress.com/#WindowsForms/CustomDocument1477
Few possibilities:
check FieldName property of edited column. Maybe there is a typo, so grid does not pass your entered value to underlying datasource
property that is bound to column must have public setter. If there is only getter, grid also won't be capable to store entered value
check ColumnOptions.ReadOnly property in grid column - must be set to false
Hope this helps

Resources