specific childrow hierarchy for radgridview - c#

I would like to iterate through the rows on my RadGridView when data binding is complete and for each row modify the columns which are shown on one of its child templates (one hierarchy level beneath).
I've tried this:
void rgvQuestions_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
bool didit = false;
foreach (var row in rgvQuestions.Rows)
{
if (!didit)
row.ViewInfo.ViewTemplate.ChildGridViewTemplates[1].Columns[0].IsVisible = false;
didit = true;
}
}
But it hides the column for all of the child gridview templates and not not only for the specific row (first row in this case).
Thanks

Each Row has a ChildRows collection where its child rows are stored:
foreach (var row in radGridView1.Rows)
{
foreach (var childrow in row.ChildRows)
{
//do smth
}
}

Related

Adding different items to DataGridViewComboBoxColumn of each row in a DataGridView

Suppose, I have this DataGridView in my application.
- Child is a combo-box column
- ChildID is a combo-box column
I have some objects named Table which I want to load in the Child-column. Each Table object has a number of Column objects which I want load in ChildID-column.
When I change a Child combo box, ChildID column should automatically change to load appropriate columns from the Table object.
The following code is my attempt:
// populate the DataGridView
if (database != null)
{
childDataGridView1Column1.Items.Clear();
dataGridView1.Rows.Clear();
string firstTableName = database.Tables[0].Name;
// Loading ComboBox columns
int i = 0;
foreach (Table t in database.Tables)
{
dataGridView1.Rows.Add(true, t.Name, t.PrimaryKeyName);//Set Child's text to "None"
dataGridView1.Rows[i].Tag = t;
// Load 'Database.Tables' to 'Child' column
DataGridViewComboBoxCell dataGridview1ChildComboBoxCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[(int)CellNo.Child];
dataGridview1ChildComboBoxCell.Items.Clear();
foreach (Table t2 in database.Tables)
{
dataGridview1ChildComboBoxCell.Items.Add(t2.Name);
}
dataGridView1.Rows[i].Cells[(int)CellNo.Child].Value = firstTableName;
// Load 'Table.Columns' to 'ChildID' column
DataGridViewComboBoxCell dataGridview1ChildIDComboBoxCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[(int)CellNo.ChildID];
dataGridview1ChildIDComboBoxCell.Items.Clear();
foreach (Column c in t.Columns.Values)
{
dataGridview1ChildIDComboBoxCell.Items.Add(c.Name);
}
i++;
}
}
... ... ...
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[(int)CellNo.Child];
//if (cb.Value != null)
{
// do stuff
DataGridViewRow row = dataGridView1.SelectedRows[0];
Table t = row.Tag as Table;
// Load 'Table.Columns' to 'ChildID' column
DataGridViewComboBoxCell dataGridview1ChildIDComboBoxCell = (DataGridViewComboBoxCell)row.Cells[(int)CellNo.ChildID];
dataGridview1ChildIDComboBoxCell.Items.Clear();
foreach (Column c in t.Columns.Values)
{
dataGridview1ChildIDComboBoxCell.Items.Add(c.Name);
}
dataGridView1.Invalidate();
}
}
But, it doesn't work.

How can I convert a List<T> to DataTable with only selected parameters

I have a List<Indicators> where Indicators has several properties.
I have a ListView which holds selected properties of that class as a list of strings.
I want to create a DataTable from the List using only the selected properties in the listview.
So far I can create the columns for the DataTable with the selected parameters in listview. I'm stuck on how to fill each row with data from selected columns only.
the for block is where I'm stuck, I know what I have there isn't right. Any help is appreciated, thanks.
internal DataTable ConvertToDataTableAll(List<Indicators> data)
{
DataTable table = new DataTable();
foreach (ListViewItem item in listviewFeatures.Items)
{
table.Columns.Add(item.Text);
//this for block should fill the current column with data.
for (int i = 0; i < data.Count; i++)
{
var row = table.NewRow();
table.Rows.InsertAt(row, i);
table.Rows[i][item.Text] = data.Select(x => item.Text);
}
}
}
Solution with some changes to CodingYoshis suggestion.
This is after adding all columns from code above and removing the for block.
foreach(Indicators ind in data)
{
var row = table.NewRow();
foreach(PropertyInfo prop in ind.GetType().GetProperties())
{
if (table.Columns.Contains(prop.Name))
{
row[prop.Name] = prop.GetValue(ind);
}
}
table.Rows.Add(row);
}
You need to add the columns once for the entire table, not for every row as you are doing. So it should be done like this:
var firstItem = listviewFeatures.Items.FirstOrDefault();
if (firstItem == null)
{
// Nothing to convert to datatable so return
return;
}
// We have items so lets initialize the table
DataTable table = new DataTable();
foreach (ColumnHeader header in listviewFeatures.Columns)
{
table.Columns.Add(header);
}
// Now lets add the rows
foreach (ListViewItem item in listviewFeatures.Items)
{
// Create one row per row in listview
var row = table.NewRow();
// Traverse the listview by each column and fill the row
foreach (ColumnHeader header in listviewFeatures.Columns)
{
row[header] = item.Text;
}
// Add row to table. It will add it to the end.
table.Rows.Add(row);
}

How to check the row color in DataGridView in C#

I want to get the DataGridView row color in C#.
I have set the background color of a row like this:
dgvGrid.Rows[rowIndex].DefaultCellStyle.BackColor = Color.LightPink;
Now I want to get only those rows whose BackgroundColor is LightPink.
foreach (DataRow dr in dgvGrid.Rows)
{
if( /* get the row whose color is pink */)
{
}
}
You were close. However, note that dgvGrid.Rows is not a collection of DataRow - which:
Represents a row of data in a DataTable.
Instead, it is a DataGridViewRowCollection which is:
A collection of DataGridViewRow objects.
After fixing this in your loop, just check the row color the same way you set it:
foreach (DataGridViewRow row in dgvGrid.Rows)
{
if (row.DefaultCellStyle.BackColor == Color.LightPink)
{
// your code here
}
}
Like this:
int index = 0;
foreach (var item in ListGV.Rows)
{
if (ListGV.Rows[index].BackColor == Color.Pink)
{
}
index++;
}

C#. Get all cell in a row by row index in a data grid

I have the row index of a table. I need to get the value of each cell in the row and add it to a list. I can't seem to get it right.
You could iterate through DataGridView rows and add specific index value to a list,
private void button2_Click(object sender, EventArgs e)
{
List<string> items = new List<string>();
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (null != item && null != item.Cells[0].Value)
{
items.Add(item.Cells[0].Value.ToString());
}
}
}

Access a cell in DataGrid.SelectedItems

I need to read a string from each of the the first cells of SelectedItems of the DataGrid
foreach (var item in myDataGrid.SelectedItems)
{
if (item[0].ToString().Contains("Buy"))
{
containsBuy = true;
}
if (item[0].ToString().Contains("Sell"))
{
containsSell = true;
}
}
How can I cast myDataGrid.SelectedItems? It is IList and gives object. Is there any simple and similar way to do it as it is done for one selected row of a DataGrid:
var row = myDataGrid.SelectedItem as DataRowView;
Here I can easy access any cell - row[i].
Just change your foreach loop to:
foreach (DataRowView in myDataGrid.SelectedItems)
{
//...
}

Categories