How to get value of cell at current row GridView winforms devexpress - winforms

I need to know which row I selected and get value of cell(Ex:idproduct) before to click Edit button.

As #brendon is referring to, if gridView is the current View on your GridControl:
// Get your currently selected grid row
var rowHandle = gridView.FocusedRowHandle;
// Get the value for the given column - convert to the type you're expecting
var obj = gridView.GetRowCellValue(rowHandle, "FieldName");

You can use the GridView's GetRowCellValue method to retrieve the focused row value.
http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridGridView_GetRowCellValuetopic
See also: http://documentation.devexpress.com/windowsforms/CustomDocument753.aspx

public int idproductx;
public void tProductGridView_RowClick(object sender, RowClickEventArgs e)
{
if (e.Clicks > 0)
{
idproductx = (int)((GridView)sender).GetRowCellValue(e.RowHandle, "idproduct ");
}
}

Related

Reset comboxbox selection after a selection is made

I have a combobox that is created with data from a dataset
foreach(var item in ds.MiDESValues)
{
string comboboxtext = ds.MiDESValues.Rows[k][1].ToString();
sFactorCB.Items.Add(comboboxtext);
k++;
}
On a selectionchanged event it will populate a listbox with that selection
private void sFactors_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string add = sFactorCB.SelectedValue.ToString();
var svalue = ds.MiDESValues.Rows[0][2].ToString();
int Svalue = int.Parse(svalue);
SValue.Add(svalue);
SelectionListBox.Items.Add(add);
SelectionBox.Add(add);
// when a new item is added to Selection list box, select it and show it
// this will keep the last item highlighted and as the list grows beyond
// the view of the list box, the last item will always be shown
SelectionListBox.SelectedIndex = SelectionListBox.Items.Count - 1;
SelectionListBox.ScrollIntoView(SelectionListBox.SelectedItem);
}
That list box then used to populate a listbox used on the next page. If I navigate to the next page and then navigate back, the combobox is still showing the last selection I made therefor the listbox is being populated with that value.
I have tried setting the selectedindex of the combobox to sFactorCB.SelectedIndex = -1;, at the end of the sFactors_SelectionChanged event but i get System.NullReferenceException. How can I get the combobox to reset back to a non-selected item state? Thanks
Actually, you are doing it correctly. To clear the selection either set the SelectedIndex to -1 or the SelectedItem to null.
The problem is that once you do that your sFactors_SelectionChanged gets called again and since there is no current selection, the SelectedValue property is null thus causing the following to fail:
string add = sFactorCB.SelectedValue.ToString();
How to solve this sort of depends on your intended results when nothing is selected. The simplest thing to do would be to just check for nothing selected at the start of the handler and simply return.
private void sFactors_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sFactorCB.SelectedIndex == -1 || sFactorCB.SelectedValue == null)
return;
string add = sFactorCB.SelectedValue.ToString();
...

How to retain Focus of the first row after sorting and searching in the datagrid?

When my datagrid loads up I am able to get the focus of the first row by providing selected index=0 in the xaml but when I perform searching the focus gets lost so I want focus to be retain at the first row no matter I do sorting and searching on the datagrid.
Here is my code that searches particular thing in the datagrid.
private void TextBox_TextChanged(object sender, RoutedEventArgs e)
{
TextBox STB = (TextBox)sender;
this.SearchValue = STB.Text;
//ContentPresenter CP = (ContentPresenter)STB.TemplatedParent;
//DataGridColumnHeader DGCH = (DataGridColumnHeader)CP.TemplatedParent;
//DataGridColumn DGC = DGCH.Column;
//this.ColumnName = DGC.Header.ToString();
this.Datalist.Filter = this.CustomeFilter;
DataGrid dataGrid = this as DataGrid;
dataGrid.CurrentCell = new DataGridCellInfo(
dataGrid.Items[0], dataGrid.Columns[0]);
dataGrid.BeginEdit();
}
In above code I am trying to get the focus of the current cell but all in vain.
private bool CustomeFilter(object item)
{
SymbolData ltpObj = item as SymbolData;
//WpfApplication1.Model.LtpMessage ltpObj = item as WpfApplication1.Model.LtpMessage;
string values = (string)ltpObj.Symbol.ToString();
values = values.ToUpper();
//return values.StartsWith(this.SearchValue.ToString().ToUpper());
if (values.StartsWith(this.SearchValue.ToString().ToUpper()))
{ return true; }
else
return false;
}
You should read this:
How to programmatically select and focus a row or cell in a DataGrid in WPF: https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/
You can select and focus a row or cell of a DataGrid programmatically and get the same behaviour as when using the mouse by accessing the visual user interface elements of the DataGrid control and calling the UIElement.Focus() method on a particular DataGridCell object as described in the blog post above. There are code samples included.
You cannot simply set the SelectedItem or SelectedIndex property of the DataGrid to focus the row or cell though.

Winform DataGridViewLinkColumn ReadOnly property not working

In my VS2015 Winform app, there is one DataGridView control bound to a BindingSource that is bound to a SQL database. The Grid has four columns: ID, URL, Name, Type. The URL column is DataGridViewLinkColumn whose ReadOnly property, by default, is set to False. I can edit the Name and Type columns but URL columns shows as ReadOnly. Why? How can I make URL column editable?
As Reza stated:
DataGridViewLinkColumn is not editable.
Therefore, to edit a cell in such a column you'll have to convert it to a DataGridViewTextBoxCell as needed. For instance, if I have subscribed to DataGridView.CellContentClick to handle clicking on a link, then I would handle CellDoubleClick for the cell conversion:
private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] == this.dataGridView1.Columns["URL"])
{
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = new DataGridViewTextBoxCell();
this.dataGridView1.BeginEdit(true);
}
}
Once you've entered your value and left the cell, you should then use CellValidated to verify that the new value is a URI before converting the cell back to a DataGridViewLinkCell:
private void DataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] == this.dataGridView1.Columns["URL"])
{
DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (Uri.IsWellFormedUriString(cell.EditedFormattedValue.ToString(), UriKind.Absolute))
{
cell = new DataGridViewLinkCell();
}
}
}
Caveat:
This only worked for me when the data for the "URL" column were strings and thus after binding, the column defaulted to a DataGridViewTextBoxColumn - forcing a manual conversion to link cells to begin with:
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (Uri.IsWellFormedUriString(r.Cells["URL"].Value.ToString(), UriKind.Absolute))
{
r.Cells["URL"] = new DataGridViewLinkCell();
}
}
}
Setting up the "URI" column as a DataGridViewLinkColumn from the beginning allowed for the conversion of cells to TextBox type successfully. But when converting back to link cells, debugging showed the conversion to happen, but the cell formatting and behavior failed.

Assigning values to array of 'Combo Boxes' in a 'Group Box' using for each loop in c#

I have 10 comboBox in a groupBox
for I just want to display a calculated value in respective comboBox like this say if I set a varible double i=08.00; then on button click cmboBox should display values like this
CB1-08.00
CB2-09.50
CB3-10.00
CB4-10.50
CB5-11.00
CB6-11.50
.... and so on upto CB10 But I am getting output like this
And Code
private void button1_Click(object sender, EventArgs e)
{
double i=08.00;
foreach (var comboBox in groupBox1.Controls.OfType<ComboBox>())
{
comboBox.Text = i.ToString("00.00");
i = i + 0.5;
}
}
Your combobox order is different in the collection so it inserts the numbers randomly. May be you can name your combobox for instance like cmb1,cmb2,cmb3 etc. and if you update your code it will run.
Your controls in the Controls collection are not sorted by their appearance on the form. You will need to find a way to sort them if you need different values in each based on their position.
Foreach loop doesn't give the collection in the order you wanted. The way to go forward is to give a tag id to each combo box, then you can use that to assign a value to them them.
So your first combo box will start with tag id 0, and the last one will have 8,
double val = 08.00;
for (int i = 0; i < groupBox1.Controls.Count; ++i)
{
var combobox = groupBox1.Controls[i] as ComboBox;
int tag = int.Parse(combobox.Tag.ToString());
double value = val + (0.5 * tag);
combobox.Text = value.ToString("00.00");
}
Make sure you tag the cobbo box in the order you wanted them.

Changing cell in GridView by vlidation

I have a GridView and I want when I change a cell to see if its new value is valid by mine function ValidateValue(string aValue) and if it is valid - to store the new value and old value as a pair in Struct S {string old,new}; How to do this?
Handle the GridView's ValidatingCell event for this purpose. Here is some sample code showing how to obtain new and old edit values:
private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) {
BaseEdit edit = (sender as GridView).ActiveEditor;
object oldValue = edit.OldEditValue;
object newValue = e.Value;
}

Resources