Retain the value entered in the cell of DevExpress Xtragrid - devexpress

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

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 GridLookUpEdit into RepositoryItemGridLookUpEdit, or GridLookUpEdit into column cell

I know I can set
//RepositoryItemGridLookUpEdit riglue
eePozycje.gvView.Columns[KolNazwa].ColumnEdit = riglue;
but all I have is GridLookUpEdit.
How can I set GridLookUpEdit into column cel, or transform GridLookUpEdit into RepositoryItemGridLookUpEdit ?
//DONE
I found it in GridLookUpEdit.Properties.
Refer the documentation
The RepositoryItemLookUpEdit class contains settings specific to the
GridLookUpEdit control. You can access these settings via the editor's
GridLookUpEdit.Properties object. See the GridLookUpEdit topic for
details on the control.
You need to create repository items as standalone objects only to
specify inplace editors for container controls (such as the XtraGrid,
XtraTreeList, etc)
I think you that How to Assign Editors for In-Place Editing. Now If you want to set editors in particular cell then you have to handle the GridView.CustomRowCellEdit. The event occurs dynamically for each visible cell and allows you to supply an editor to individual cells, based on the position of the cell (its column and row).
Refer this - Assigning Editors to Individual Cells
example:
using DevExpress.XtraGrid.Views.Grid;
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
if (e.Column.FieldName == "FieldName") return;
GridView gv = sender as GridView;
string fieldName = gv.GetRowCellValue(e.RowHandle, gv.Columns["FieldName"]).ToString();
switch (fieldName) {
case "Population":
e.RepositoryItem = repositoryItemSpinEdit1;
break;
case "Country":
e.RepositoryItem = repositoryItemComboBox1;
break;
case "Capital":
e.RepositoryItem = repositoryItemCheckEdit1;
break;
}
}
Hope this help.

How can i select Datakey value of a grid

How can i select Datakey value of a grid .? I tried but only getting inside selected index changed event.
[In my application there is a grid.In that grid there link buttons.My issue is when i am clicking on link button i want to acceess value in Datakeynames. is there any method to access value in Datakeynames. or is there any other property for grid to keep key values ].Please help
You have your answer at below link
How to get the Command Argument on Row Data bound Event of gridview
Happy coding!!!
Try like this,
Assign your Value in the Command Argument.
In RowCommand Event,
if (e.CommandName == "Link")
{
string Value = e.CommandArgument.ToString();
}

ASP Multiselect listbox separator

I have encountered a problem and I didn't manage to find any soultions yet. Let me simplify things a bit.
I have 2 forms, the first contains an ASP ListBox with multi select mode enabled. I submit the form and in the other form I use just for testing purposes this snippet of code:
protected void Page_Load(object sender, EventArgs e)
{
foreach (string formKey in Request.Form.AllKeys)
{
if (formKey != null)
{
if (formKey.Equals("ctl00$MainContent$ListBox1"))
Label1.Text = Request.Form[formKey];
}
}
}
The problems is that the values that come from the listbox (the values that i selected in the previous form) are separated by "," for ex. "test1,test2,test3". How can i change this separator to "$" for example? I need to change it because the actual values may contain "," and i don't manualy feed them to the listbox.
I can't use any other mode of transfering this values between the form because the entire application uses this model. The values that i get are then sent to a workflow where there will be manipulated and in the workflow i need to know where each listbox item starts and ends so it must be an unique separator.
Any help is apreciated! Thank you very much
Thank you MatteKarla but unfortunately this does not solve my problem. Yes, this is a good way of transfering the values from one form to another.
However i must use the method I described above with Request form keys because the listbox is one of many others "parameters" that are generated at runtime and have their values sent to a workflow method that takes this values. And i can't afford to change that in my application.
My problem is that coma (",") separator is used by default with a multiselect listbox.
I thought that there maybe is a method to change that separator from coma to another char because the coma can also be included in the value itself and this will create confusion.
As i said if i select three values test1, test2 and test3, the result with my method will be a string looking like "test1,test2,test3". However a "test1$test2$test3" would be much better.
But I'm affraid that changing this default separator is not possbile. I must think at a method to overcome this problem like replacing before feeding the listbox all the intended coma from the values with some other char not to create confusion. But this is not a great way of doing it.
On your first page/form (First.aspx.cs) create a public property with the listbox:
public ListBox PostedListBox { get { return ListBox1; } }
Set the postback-url for the button to Second.aspx
Second page in the aspx-file after the #Page-directive add:
<%# PreviousPageType VirtualPath="~/First.aspx" %>
Then in Form_Load on Second.aspx.cs you can extract the values:
if (PreviousPage != null)
{
ListBox postedListbox = PreviousPage.PostedListBox;
foreach (var index in postedListbox.GetSelectedIndices())
{
var itemText = postedListbox.Items[index].Text;
}
}
Or you could just try to locate the control by using:
if (PreviousPage != null)
{
var control = PreviousPage.FindControl("ListBox1") as ListBox;
}
Third Edit:
You could use GetValues:
Request.Form.GetValues("ctl00$MainContent$ListBox1");
returns a string array containing each of the selected items.

How to Get row by key value or visible index in ASPxGridView then change column value?

Hi
In ASPxGridView, is there a way to get a row by its VisibleIndex or KeyValue so that I can change any column value in it?, I mean something like this:
var row = myGrid.SelectRowByKeyValue(myKeyValue);
OR:
var row = myGrid.SelectRowByVisibleIndex(myKeyValue);
row["Column1"] = true;
Edit:
What I'm tring to do is that every time I hit the button I want to check one specific row (I'm using ajax to not reload all the page);
Thanks
This can be done using the ASPxGridView.GetRow() method. NOTE, that changing the value in the DataRow is not enough. If you want these changes to be preserved, save them to the DB.
Since you are using unbound columns, you should handle the CustomUnboundColumnData event and provide modified data for this row within this event handler. The common approach is described in the Providing Data for Unbound Columns topic. If this does not help, please describe in greater details.
UPDATE
Your approach is incorrect. The ASPxGridView does not provide a method to set a text of a certain cell (TD). Instead, you should force the grid to raise the CustomUnboundColumnData event. This can be done using the ASPxGridView's DataBind method. In this event handler, you should determine the KeyField value of the processed row, compare it with the keyField value of the row where the button was clicked and return the required value. This is how I would implement this feature...
I solved it by using this code:
for (int i = 0; i < myGridView.VisibleRowCount; i++)
{
if ( [My condition] )
{
(
(CheckBox)myGridView
.FindRowCellTemplateControl(i,
myGridView.Columns["MyColumnName"] as GridViewDataColumn,
"My_Unbound_Control_Name"
)
).Checked = true;
}
}
I's may not be the right way to do it but I couldn't solve it another way.

Resources