Access a cell in DataGrid.SelectedItems - c#

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)
{
//...
}

Related

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++;
}

Dropdownlist not highlighting the given value

i have a dropdown list which i want to highlight a item from it. i have given the condition correct as par to me.But it didnt highlight the given item,instead showing normally as other items.
DataTable dtt = new DataTable();
dtt.Load(cmd.ExecuteReader());
ddlCompanyName.DataSource = dtt;
ddlCompanyName.DataTextField = "COMPANYNAME";
ddlCompanyName.DataValueField = "COMPANYID";
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
ddlCompanyName.DataBind();
ddlCompanyName.Items.Insert(0, new ListItem("--Select Name--"));
Compidd(string) has specified item to be highlighted in dropdownlist
You need to do DataBind before the loop:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
EDIT:
To set the value as default value you can try like this
ddlCompanyName.SelectedValue = "The value which you want to set as default"
The ddlCompanyName.DataBind(); must be executed before you loop the items:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
Otherwise there are no items in the DropDownList.

specific childrow hierarchy for radgridview

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
}
}

Finding datagridview cell type in C#?

i am trying to find each DatagridviewImageCell and set its property ImageLayout to DataGridViewImageCellLayout.Zoom so the image in that cell will be zoomed. I am using this code, but am getting an error: Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.Windows.Forms.DataGridViewImageCell'. here: (DataGridViewImageCell Imgrow in dataGridView1.Rows. This is the code i am using.
foreach (DataGridViewImageCell Imgrow in dataGridView1.Rows)
{
if (dataGridView1.Rows[a].Cells[1].Value == "Image")
{
Imgrow.ImageLayout = DataGridViewImageCellLayout.Zoom;
}
}
How do i fix it? Also, the column is a texbox column, but i am using this to replace the cell.
int a = 0;
dataGridView1.Rows.Insert(0, 1);
dataGridView1.Rows[a].Cells["Column1"] = new DataGridViewImageCell();
dataGridView1.Rows[a].Cells["Column1"].Value = picturebox1.Image;
You need to loop over the rows with a row object, and then loop over the cells with a cell object.
Something like this:
foreach (DataGridViewRow dr in dataGridView1.Rows) {
foreach (DataGridViewCell dc in dr.Cells) {
if (dc.GetType() == typeof(DataGridViewImageCell)) {
((DataGridViewImageCell)dc).ImageLayout = DataGridViewImageCellLayout.Zoom;
}
}
}

Categories