I have a GridLookUpEdit and wath to get a edit value on FocusedRowChanged event:
private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
var view = sender as GridView;
if (view != null)
{
MessageBox.Show(view.GetRowCellValue(e.FocusedRowHandle, view.Columns[1]).ToString());
}
}
But here i get a error says that i out of column array. But i have a two columns, first column visible, second not visible.
Whats wrong here? And how can i get EditValue more correctly if possible?
It seems that your view has only one column and there are two columns in your underlying datasource. So, you can get a value from underlying datasource.
If your underlying datasource is DataTable then you can use ColumnView.GetDataRow method:
MessageBox.Show(view.GetDataRow(e.FocusedRowHandle)[1].ToString());
If your underlying datasource is List<SomeObject> then you can use ColumnView.GetDataSourceRowIndex method:
MessageBox.Show(YourList[view.GetDataSourceRowIndex()].YourColumn.ToString());
Or you can add second column by using ColumnView.Columns collection:
var column = view.Columns.AddField("YourField");
column.Visible = false;
Related
I'm trying to set DataSource to DataGridViewComboBoxColumn of my DataGridView. First I'm trying to bind a data source to my DataGridView, where bindingList is a List of my custom class Plugin with properties Name (string), Id (string) and Dependencies (List):
var bindingList = PluginsHandler.GetPlugins();
var source = new BindingSource(bindingList, null);
pluginsDataGridView.AutoGenerateColumns = false;
pluginsDataGridView.DataSource = source;
pluginsDataGridView.Columns["pluginName"].DataPropertyName = "Name";
pluginsDataGridView.Columns["pluginID"].DataPropertyName = "Id";
So I can set my first two columns, but now I want to bind data to a third column of type DataGridViewComboBoxColumn. I try to do it on DataBindingComplete event:
private void pluginsDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < pluginsDataGridView.Rows.Count; i++)
{
var comboCell = (DataGridViewComboBoxCell) pluginsDataGridView.Rows[i].Cells["pluginDependencies"];
var entry = pluginsDataGridView.Rows[i].DataBoundItem as IPlugin;
comboCell.DataSource = entry.Dependencies;
}
}
Sadly comboBox is empty. Funny thing happens when I incorrectly put these lines after the first block of code I posted:
var dependenciesColumn = (DataGridViewComboBoxColumn) pluginsDataGridView.Columns["pluginDependencies"];
dependenciesColumn.DataPropertyName = "Dependencies";
Then binding seem to start to work, as I can see that there are some entries in comboboxes, but when I try to hover mouse on combobox, I am getting an error that says DataGridViewComboBoxCell value is not valid).
How can I make it work?
Instead of assigning each ComboCell a data Source, set the DataSource of the column. I assume the Dependencies property of PlugIn class is a List<string>.
pluginsDataGridView.Columns["pluginDependencies"].DataSource = //list of dependencies
You have to set the DataPropertyName of the Dependencies ComboBoxColumn to get the initial values. If you don't set it you won't see any value in the column when the application is loaded.
pluginsDataGridView.Columns["pluginDependencies"].DataPropertyName = "Dependencies"
Edit:
You have a list of dependencies for a plug-in. i.e, more than one value. Usually, to select one value from list of values, you associate list with ComboBoxColumn. Achieving your requirement of multiple values from a list using standard ComboBoxColumn is difficult. Write a custom CheckedComboBoxColumn where you can display and select multiple values.
I've got a DataGrid where the datasource is bound with a SqlDataReader object:
SqlDataReader areader = runner.Reader;
dgSearchResults.DataSource = areader;
dgSearchResults.DataBind();
I've created an ItemDataBound event for my grid and I want to check the Value of a specific Column/Cell that while each item is bound so I can enable/disable some flags.
How can I get the value of a specific cell "SvcID" on the ItemDataBound?
Here is my code:
public void dgSearchResults_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (ViewState["SvcIDs"] != null)
{
if (e.Item != null)
{
var svcIds = (List<int>) ViewState["SvcIDs"];
if (svcIds.Contains(Convert.ToInt32(**DataGrid SvcID Goes Here**))
{
//TODO: enable/disable some icons
}
}
}
}
I've used RowDataBound events before but not for a DataGrid control, and the required steps are a bit different it seems.
What is the code to check the value of Column "SvcID" in my DataGrid against my SvcIds (using an Index) List?
Thanks
You can use the following to access the cells:
e.Item.Cells[index].Text;
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagriditemeventargs.item.aspx
if your text is in second cell.
ex
e.Item.Cells[2].Text
Unfortunately you cant index the Cell collection using a column name or the databound field name.
To do that you need to know the index of the datafield (e.g. SvcID in your case) and index the Row object. If you are using AutogenerateColumns then the index would be based on the order of columns you are querying from the database.
Then you can handle the RowDataBound event, which has an event argument of type GridViewRowEventArgs that gives u access to the DataGridView row instance. then you can do something like this:
string val= e.Row.Cells(index).Text;
I am trying to assign different tooltips to DataGrid rows according to certain conditions.
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
DataGridRow row = e.Row;
if (row["AssemblySummary"].ToString.Contains("Class"))
{
row.ToolTip = "Class definition...";
}
}
I am getting "Cannot apply indexing to an expression of type 'System.Windows.Controls.DataGridRow'." If i have a row why cannot i access its items? How to access cell[i]?
Edit made: I am verry sorry, I needed to access first cell in the given row like it is done now. I still have the same error. Thank you
Try this and let us know if it works:
DataGridRow row = e.Row;
DataRowView rView = row.Item as DataRowView
if(rView != null && rView.Item["AssemblySummary"].ToString().Contains("Class"))
row.ToolTip = "Class definition...";
If it doesn't, then it could be that the visual row has been loaded, but the data hasn't been bound yet.
You can get the data of the row using the DataContext property.
Try this link http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html
I have few columns in my DataGridView, one of them is an unbound column and the DataGridVIew is in VirtualMode.
When CellValueNeeded event is called, I want to calculate value of Cells[0] basing on the value of Cells[2] which is in bounded column to the underlaying DataSource.
This is how I try to do this:
private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
e.Value = dgvItems.CurrentRow.Cells[2].Value * 5; //simplified example
}
However, I am getting System.StackOverflowException because it seams that call to dgvItems.CurrentRow.Cells[2].Value results in call to another CellValueNeeded event. And so on and so on... However Cells[2] is not an unbound column, so on common sense it should not result in recursive call unless getting value of any column(bound or unbound) firest that event...
I can not use here SQL Expression and I can not precalculate e.Value in any SQL call. In real example Cells[2].Value is a key used in HashTable which will return a correct value for the Cells[0] (e.Value).
What can I do?
You need to be selective and act on column 0 only:
//untested
private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
if (e.Column == 0)
{
e.Value = dgvItems.CurrentRow.Cells[2].Value * 5; //simplified example
}
}
I have a form with a DataGridView (of 3 columns) and a Button. Every time the user clicks on a button, I want the get the values stored in the 1st column of that row.
Here is the code I have:
private void myButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in ProductsGrid.Rows)
{
if (this.ProductsGrid.SelectedRows.Count == 1)
{
// get information of 1st column from the row
string value = this.ProductsGrid.SelectedRows[0].Cells[0].ToString();
}
}
}
However when I click on myButton, the this.ProductsGrid.SelectedRows.Count is 0. Also, how do I ensure that the user selects only one row and not multiple rows?
Does this code look right?
Set DataGridView.MultiSelect=false and DataGridView.SelectionMode = FullRowSelect. This will make it so the user can only select a single row at a time.
SelectedRows only returns the rows if the entire row is selected (you can turn on RowSelect on the datagridview if you want). The better option is to go with SelectedCells
private void myButton_Click(object sender, EventArgs e)
{
var cell = this.ProductsGrid.SelectedCells[0];
var row = this.ProductsGrid.Rows[cell.RowIndex];
string value = row.Cells[0].Value.ToString();
}
Well, you don't need to both iterate over all rows in your grid and access the collection of SelectedRows. If you skip iteratating and use the SelectedRows collection, then your problem is probably an incorrect SelectionMode:
The SelectionMode property must be set
to FullRowSelect or RowHeaderSelect
for the SelectedRows property to be
populated with selected rows.
(from MSDN)
You can reference the grid similar to an array:
ProductsGrid[ProductsGrid.SelectedColumns[0].Index, ProductsGrid.SelectedRows[0].Index].Value;
By selecting the indexes from the first index of the SelectedRowsCollection and SelectedColumnsCollection you'll grab the first value if multiple rows are selected.
You can lock the user to selecting only a single row by setting the MultiSelect property on the DataGridView. Alternatively you make the CellClick event perform:
ProductsGrid.ClearSelection();
ProductsGrid.Rows[e.RowIndex].Selected = true;
SelectedRows.Count returns the number of entire rows that are currently selected. You probably want to use SelectedCells.Count.
you can also use the .BoundItem