Check checkboxes in DataGridView programmatically - c#

I have seen a few posts similar to this issue, but I haven't come up with my answer so, I thought I would float it again to see what comes up...
I am using ExcelDNA to integrate an API with Excel using C#.NET. I have a DataGridView, and I would like to check items that already exist on a list.
The following code works when tied to a button-click event, but NOT when the code is called in a method.
private void SelectAllItems()
{
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"];
check.Value = check.TrueValue;
}
}
I am running into the same issue elsewhere, too:
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"];
string hid = row.Cells["Hid"].Value.ToString();
if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0)
{
check.Value = check.TrueValue;
}
}
I've been researching this for a few hours, coming up completely empty. Any help would be greatly appreciated.

You can do this by setting the value to true. You do not need to cast to a checkbox cell.
private void SelectAllItems()
{
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
// This will check the cell.
row.Cells["selectItem"].Value = "true";
}
}
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
string hid = row.Cells["Hid"].Value.ToString();
if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0)
{
row.Cells["selectItem"].Value = "true";
}
}

Related

Can't change color of specific rows in a DataGridView. DefaultCellStyle.BackColor not working

I'm trying to change the color of some specific rows in a DataGridView, but for some reason DefaultCellStyle.BackColor is not working. If I change the color of the whole table, it works. But if I try to do it to a single row, it just doesn't work. This is the code I wrote:
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToString(row.Cells[20].Value) != "")
{
MessageBox.Show("Inside if"); //With this messagebox I make sure I get inside the if.
row.DefaultCellStyle.BackColor = Color.Red;//This just doesn't work.
}
}
I also tried to change the color of just one row, but had no luck.
dataGridView2.Rows[1].DefaultCellStyle.BackColor = Color.Red;
I don't know what am I missing or if I'm just too dumb to see the problem. I've already searched in different forums for an answer but I couldn't find any.
The datagridview control cannot change colors until the form has been shown. If you have this in the Load event, move it to the Shown event.
OR
You can try putting it in the DataGridView_CellFormatting event like this:
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// Assuming 20 is the column index you're looking for
if (this.dataGridView2.Columns[e.ColumnIndex].Index == 20)
{
if (e.Value != null)
{
string strVal = (string)e.Value;
if (strVal != "")
{
e.CellStyle.BackColor = Color.Red;
}
}
}
}

DataGridView Row Color Doesn't Change

Hello im working on Windows Form Applications but i have a problem. We are using data grid view and if row's if one column or more is empty, i want to highlight it. I dont know why but my code doesn't work. Here its my code;
public Form1()
{
InitializeComponent();
var dtCombined = PopulateCombinedDatatable();
dataGridView.DataSource = dtCombined;
HighlateIfEmpty();
}
public string[] FindFilePath()
{
//OPERATIONS
}
public DataTable PopulateCombinedDatatable()
{
//MY OPERATIONS
}
public void HighlateIfEmpty()
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if ((string)cell.Value == string.Empty)
{
cell.Style.BackColor = Color.BlueViolet;
cell.Style.SelectionBackColor = Color.Aquamarine;
row.DefaultCellStyle.SelectionBackColor = Color.BlueViolet;
row.DefaultCellStyle.ForeColor = Color.Yellow;
row.DefaultCellStyle.BackColor = Color.Aquamarine;
}
}
}
}
Thanks...
PS : This code finds right columns and rows but not paint it
You should call this HighlateIfEmpty() in dataGridView1_CellFormatting event,
for your reference i added a link please go through it.
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx
I know this post is a bit old, but anyway . . .
On the DataGridView there is a DefaultCellStyle, inside this there is SelectionBackColor and SelectionForeColor properties.
The DataGridView uses a style inheritance idea, in case you find that the style you pick is not being applied.

Hide a cell from gridview

I have a winform c# SQL app in which i retrieve some values to a datagrid view and from there on wards i will display them to a user.
however there are certain gridcells which have values that i don't want to show to the user, and i want to hide them,
here is my code to hide cell values.
CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource=dmz.Tables[0]];
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells[51].Value.ToString() == "N/A") //object reference not set to an instance of the object exception is thrown
{
cm.SuspendBinding();
dgvr.Visible = false;
}
there is an object reference not set to an instance of the object exception thrown...
what is triggering the error?
Please help...
Change
if (dgvr.Cells[51].Value.ToString() == "N/A")
to
if (dgvr.Cells[51].Value??"").ToString() == "N/A")
or
if (dgvr.Cells[51].Value!=null && dgvr.Cells[51].Value.ToString() == "N/A")
You can use the DataGridView_CellFormatting event method .
Like this:
private void dgvr_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(dgvr.Rows[e.RowIndex].Cells["51"].Value!=null && dgvr[51].Value.ToString()=="N/A")
{
dgvr.Rows[e.RowIndex].Cells["51"].Value="";
}
}

Loop through a DatagridView and check if string exist in first cell

private void cap_CapturingEventHandler(string file)
{
this.Invoke((MethodInvoker)delegate
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (file == row[0].ToString())
{
}
}
});
}
My application adds files into my DatagridView. Each time a new file arrived i want to check whether this file is already exist in my DatagridView (first cell in row).
It seems that my example does not even compile.
Change the following statement
row[0].ToString()
to
row.Cells[0].Value.ToString()

Move item from listbox1 to listbox2 in web application

I have 2 listboxes. One contains all the department which are unassigned and other listbox contains all the department which is assigned to a particular person. Now I want to add and delete departments using insert and delete query. The query will be performed on 1 table only ie Assigned Department(listbox2) and by clicking the SAVE button. I have done the insert part but unable to handle the delete part. I have seen few examples but they dont have DB event in them.
protected void Save_Click(object sender, EventArgs e)
{
DataTable dt = GetData2();
bool found = false;
foreach (RadListBoxItem item in RadListBox2.Items)
{
found = false;
foreach (DataRow dr in dt.Rows)
{
if (String.Compare(item.Value, dr["DeptID"].ToString()) == 0)
{
found = true;
label1.Text = "Add a New Group to the ListBox";
}
}
if (found == false)
{
Item(item.Text, item.Value);
}
This is what I am trying to do. I want to handle insert and delete event on Save button.
I figured out the solution so I am posting it in here.
foreach (DataRow dr in dt.Rows)
{
found = false;
foreach (RadListBoxItem item in RadListBox2.Items)
{
if (String.Compare(item.Value, dr["DeptID"].ToString()) == 0)
{
found = true;
}
}
if (found == false)
{
//delete here
}
}
}
I use the RadListBox's Transferred event. When the Transferred event fires Items look at the destination listbox's id and determine if the items were moved to the unassigned or assigned ListBox. Once you know the destination you can execute a query to add or remove rows from your database.
In any case, your Query will be dependent on your schema. In my situation I had a lookup table that determined if the item was attached.
rlistAvailable_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
{
if (e.DestinationListBox.ID == "rlistAttached")
{
foreach (Telerik.Web.UI.RadListBoxItem li in e.Items)
{
//do insert query using li.Value
}
}
else
{
foreach (Telerik.Web.UI.RadListBoxItem li in e.Items)
{
//do delete query using li.Value
}
}
}

Categories

Resources