getdatasourcerowindex nullreference after filter using gridview (Devexpress Winform)? - winforms

I'm using gridview devexpress. After i use filter on that component, there is error nullreference. If i don't using filter. There isn't error on my code.
for (int i = 0; i < GridView1.RowCount; i++)
{
DataRow dr;
dr = GridView1.GetDataRow(GridView1.GetDataSourceRowIndex(i));
MessageBox.Show(dr[0].ToString());
}
That is my code. Any solution to get datarow value from gridview after filter?

Maybe the problem is that you are using RowCount. The DataSourceRowIndex is based on the index your underlying datasource holds. Thats not the same. If you are grouping or sorting the rowhandle of gridview is changing. And the RowCount only shows you how much rows are in the GridView. But this must not be equivalent to your datasourceindex.
try the following to get your datarow:
1.
foreach (DataRow row in ((DataTable)GridControl1.DataSource).Rows)
{
//Here you can access your row via row variable
}
You have to know that the RowCount Property cant be used for accessing rows safely!
If you need further help dont hesitate for asking.

You must use BaseView.DataRowCount property instead of BaseView.RowCount property:
for (int i = 0; i < GridView1.DataRowCount; i++)
{
DataRow dr;
dr = GridView1.GetDataRow(GridView1.GetDataSourceRowIndex(i));
MessageBox.Show(dr[i].ToString());
}

Related

WPF hide row in datatable or in datagrid

I created datatable and fill it with columns and rows then i "add" this datatable to datagrid and my question is:
How to hide one row? In datatable or datagrid it doesnt matter for me. Its possible to do that?
In Windows Forms was something like CurrencyManager and that do all the job.
Okay, I found a wayto hide rows:
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator
.ContainerFromIndex(i);
Console.WriteLine(row.GetIndex());
// Console.WriteLine(row.Item);
Console.WriteLine((row.Item as DataRowView).Row.ItemArray[3].ToString());
string plec = (row.Item as DataRowView).Row.ItemArray[3].ToString();
if (plec == "M")
{
row.Focus();
row.Visibility = System.Windows.Visibility.Collapsed;
}
}
But when rows are Collapsed after click on column header to asceding or descending my datagrid show all rows(collapsed rows too).
How to block this and use filter only on visble rows?

Immediately update DataGridViewCheckBoxCell

I am programmatically updating my WinForm DataGridView
Problem, DataGridViewCheckBoxCell doesn't get updated !!!
I was google, it seams like knowing case but whatever I've tried did not help yet.
private void InitializeFunctionsDataGrid()
{
System.Data.DataSet ds = func.GetFunctions();
this.FunctionsDataGrid.DataSource = ds.Tables[0];
this.FunctionsDataGrid.Columns["FunctionId"].Visible = false;
this.FunctionsDataGrid.Columns["DESCRIPTION"].Width = 370;
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.Name = "enable";
column.HeaderText = "enable";
column.FalseValue = 0;
column.TrueValue = 1;
FunctionsDataGrid.Columns.Add(column);
foreach(DataGridViewRow row in FunctionsDataGrid.Rows)
{
(( DataGridViewCheckBoxCell)row.Cells["enable"]).Value = 1;
}
FunctionsDataGrid.CurrentCell = null;
}
enable is an unbound column. This means that you need to provide cell value yourself.
You can set the VirtualMode property to true and handle the CellValueNeeded event.
If you want to enable the user to check a cell then you need to handle the CellValuePushed event.
DataGridView samples that are part of the DataGridView FAQ has a specific example of an unbound checkbox column along with databound columns.
OK basically easiest way for me was to work with datasource.
I've add the column to the DataTable and fill it with data.
And then last thing this.FunctionsDataGrid.DataSource = ds.Tables[0];

Set number of row to display in Datagridview C#

Hi programmers out there,
I need to create a Datagridview with specified row to display almost like excel gridview. I'm able to create it in vb,but not in C#.
In C# the row will be only inserted if there's data but not by default. For example i need to create 10 rows by default. Can someone help me with this please. Any help will be most of honor.
you can use this :
public void datagridview_datasource(DataGridView dgv,personne[] _lp)
{
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
foreach (System.Reflection.PropertyInfo prop in _lp[0].GetType().GetProperties())
dgv.Columns.Add(prop.Name.ToString(), prop.Name.ToString());
int j;
for (j = 0; j < i; j++)
dgv.Rows.Add(_lp[j].nom, _lp[j].prenom);
}
Try this code
dataGridView1.Rows.Add(10);

How to get the selected column count in datagrid

I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).
I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.
The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.
Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.
// Get the Row Count
int rowCount = myGrid.Items.Count;
// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:
List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);
for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex)
{
if (!selectedColumnIndexes.Contains(column.DisplayIndex))
{
selectedColumnIndexes.Add(column.DisplayIndex);
}
}
}
}
Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.
Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.

how to get selected items in telerik radlistview

With normal winform controls I would do something like this:
ListView.SelectedListViewItemCollection col = listView1.SelectedItems;
foreach (ListViewItem item in col)
{
label8.Text = item.SubItems[1].Text;
label9.Text = item.SubItems[3].Text;
}
but I cant seem to create the samething with telerik radlistview, any ideas?
You should be able to do something similar with radlistview. The class you need to use is Telerik.WinControls.UI.ListViewDataItem.
Telerik.WinControls.UI.ListViewSelectedItemCollection col = listView1.SelectedItems
foreach (ListViewDataItem item in col)
{
label8.Text = item[1].ToString();
label9.Text = item[3].ToString();
}
I'm not sure if the ToString() is necessary. I tried it without the ToString(), and it worked fine for me, but my objects are strings.
Following Telerik Documentation
You can choose one of three methods to get or set values in subitems of RadListView:
- by using the column index
- by using the column name
- by using a column reference
Examples:
item[0] = "CellValue1";
item["Column2"] = "CellValue2";
item[radListView1.Columns[2]] = "CellValue3";

Resources