I have the following code:
foreach (DataGridViewRow row in form1.dataGridView1.SelectedRows)
{
int index = dataGridView2.Rows.Add(row.Clone() as DataGridViewRow);
foreach (DataGridViewCell cell in row.Cells)
{
dataGridView2.Rows[index].Cells[cell.ColumnIndex].Value = cell.Value;
}
So I copy rows which were selected in a GridView to another Gridview. Now I want him to check if a row is already added to the second one and open a messagebox. How to do that? I haven't found solutions which fit in my case.
foreach (DataGridViewRow row in form1.dataGridView1.SelectedRows)
{
bool isnotexist = true;
foreach (DataGridViewRow rowgrid2 in dataGridView2.Rows)
{
if (rowgrid2.Cells[0].Value.ToString() == row.Cells[0].Value.ToString())
{
isnotexist = false;
break;
}
}
if (isnotexist)
{
int index = dataGridView2.Rows.Add(row.Clone() as DataGridViewRow);
foreach (DataGridViewCell cell in row.Cells)
{
dataGridView2.Rows[index].Cells[cell.ColumnIndex].Value = cell.Value;
}
}
}
Hope this code help...
Related
I have a 15x15 dataGridView which on creation has empty (has no values) cells. Then, after user input the data, I want to get the column header which has non empty data in its row. I've been trying the following code:
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
bool empty = true;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[col.Index].Value!=null)
{
empty = false;
break;
}
}
if (empty == false)
{
dt.Columns.Add(col.HeaderText);
}
}
However, it doesn't appear to be working.
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
bool empty = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[col.Index].Value.ToString() == string.Empty)
{
empty = true;
}
break;
}
if (empty == false)
{
dt.Columns.Add(col.HeaderText);
}
}
My requirement : In datagridview I need select row by clicking the row header and selected row should maintain until I will click the other row header also same time I should select cell too.
My Problem : I can't select multiple row using Shift and Ctrl key.
my code :
List< DataGridViewRow> selectedRows = new List< DataGridViewRow>();
void selectRows()
{
dataGridView1.SuspendLayout();
foreach (DataGridViewRow r in dataGridView1.Rows)
{
r.Selected = selectedRows.Contains(r);
}
dataGridView1.ResumeLayout();
}
private void dataGridView1_RowHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
DataGridViewRow clickedRow = dataGridView1.CurrentRow;
if (selectedRows.Contains(clickedRow))
{
selectedRows.Remove(clickedRow);
}
else
{
selectedRows.Add(clickedRow);
}
selectRows();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((row.Index != e.RowIndex) && !row.Selected)
{
row.DefaultCellStyle.BackColor = Color.White;
}
else
{
selectedRows.Remove(clickedRow);
row.Selected = true;
row.DefaultCellStyle.BackColor = Color.Blue;
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.DefaultCellStyle.BackColor == Color.Blue)
{
row.Selected = true;
}
}
}
You have to set your enable your datagridview's multiselect dataGridView.MultiSelect = true;
Be familiar with using a debugger, you will be able to find out where the issue is.
You are clearing the selection in your loop
foreach (DataGridViewRow row in dataGridView1.Rows)
{
}
Rethink the if-else logic in it and you will see why. You are clearing your previous selections when you are not suppose to.
Thanks StackOverflow !
The Below code is intended to selected and run through each row.
foreach (DataGridViewRow row in RGV.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (RGV.SelectedColumns.Contains(cell.OwningColumn))
{ row.Cells["Status"].Value = "OK"; }
else
{ row.Cells["Status"].Value = "Check"; }
}
}
There is couple of errors in your code. First, you are trying to assign a DatagridViewColumn to a DatagridViewRow, which is build-able but i really dont know how it will react.
As answer to your question i would suggest something like this:
foreach (DataGridViewRow row in RGV.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
row.Cells["Status"].Value = "Check";
if (RGV.SelectedColumns.Contains(cell.OwningColumn))
{
row.Cells["Status"].Value = "OK";
break;
}
}
}
I hope i helped you somehow.
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;
}
}
}
See code below:
foreach (DataGridViewRow r in dgvStatus.Rows)
{
foreach (DataGridViewCell c in r.Cells)
{
c.Value = "";
}
}
Pretty simple, clears all the values. But I need to make some modifications to it and don't know how to go along.
I want it to clear all the values, except for the first cell in every column. Also, some cells contain checkboxes and not textboxes, how can I make a check for this?
Thanks
Try this:
foreach (DataGridViewRow r in dgvStatus.Rows) {
if (r.Index == 0) {
continue;
}
foreach (DataGridViewCell c in r.Cells) {
if c.OwningColumn is DataGridViewCheckBoxColumn) {
continue;
}
c.Value = "";
}
}