I posted something like this yesterday but think maybe my wording wasn't great since I usually get multiple answers within minutes and i got nothing.
I work in a C# environment using VS 2008. I work with datagridviews well enough, and can work with them using either buttons (DataGridViewButtonColumn) or checkboxes (DataGridViewCheckBoxColumn) without any issues. I've used the buttons to fire simple actions via the "CellContentClick" functionality. I've used the checkboxes to check selected items and then using a button outside the datagridview but still in the form to run bulk processes. My code is at the very bottom of this post.
I'm now faced with the task of creating a datagridview that have both; so I'll have column of buttons (DataGridViewButtonColumn), a column of checkboxes (DataGridViewCheckBoxColumn) and then a button otuside the DGV that references the checkbox columns.
What I'm left with is that I can't figure out how to work around the CellContentClick even that fires when I click the check box, as I want nothing to fire when i click the checkbox. I have made the underlying code skip the code in CellContentClick by recognizing it's not the correct column to click for the button, but the checkbox never gets selected because the CellContentClick fires before it recognizes the box has been checked.
Anyone have any code snippets, links or suggestions. I'm not too familiar with DGV's outside the basics but realize I really need to learn.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Data.Odbc;
using System.Data.SqlClient;
using System.Collections;
namespace AmortClient
{
public partial class frmAmortControl : Form
{
public frmAmortControl()
{
InitializeComponent();
}
public void LoadGrid()
{
object[] theRow = null;
StringBuilder sql = new StringBuilder();
// Data objects are unmanaged code.
// Declare them above the try{} block and always dispose of them in finally{}.
SqlCommand cmd = null;
SqlDataReader dr = null;
try
{
grd1.Rows.Clear();
cmd = util.SqlConn.CreateCommand();
sql.Length = 0;
sql.AppendLine("select ");
sql.AppendLine(" i.import_control_key, i.is_initial_dsc, i.stat_mo, loaded_total = round(i.loaded_total,0), ");
sql.AppendLine(" a.amort_control_key, m.amort_mode_description, a.time_stamp, a.amort_status, loaded_total_excl_NDC = round((i.loaded_total-i.NDC_Total),0), amort_total = round(a.amort_total,0), diff_total = round((i.loaded_total-i.NDC_Total),0) - round(isnull(a.amort_total,0),0) ");
sql.AppendLine(" from zstbl_import_control i ");
sql.AppendLine(" left outer join zstbl_amort_control a on i.import_control_key = a.import_control_key ");
sql.AppendLine(" left outer join tbl_amort_mode m on a.amort_mode_key = m.amort_mode_key ");
sql.AppendLine(" order by i.import_control_key desc, a.amort_control_key desc ");
cmd.CommandText = sql.ToString();
dr = cmd.ExecuteReader();
while (dr.Read())
{
theRow = new object[dr.FieldCount];
for (int ii = 0; ii < dr.FieldCount; ii++)
{
if (dr.GetName(ii).IndexOf("_total") > 0)
theRow[ii] = double.Parse(dr[ii].ToString());
else if (dr.GetName(ii).ToUpper()=="TIME_STAMP")
theRow[ii] = DateTime.Parse(dr[ii].ToString());
else
theRow[ii] = dr[ii].ToString();
}
grd1.Rows.Add(theRow);
}
dr.Close();
}
catch (Exception ex)
{
util.LogError(ex);
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
if (dr != null) dr.Dispose();
if (cmd != null) cmd.Dispose();
}
}
private void grd1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
int amortControlKey = int.Parse(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[4].Value.ToString());
string msg = "";
dgv.Rows[dgv.SelectedCells[0].RowIndex].Selected = true;
switch (dgv.Columns.Count - e.ColumnIndex)
{
case 2:
{
msg = "Are you sure that you want to delete amortization " + amortControlKey + "?";
if (MessageBox.Show(msg, "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.DoEvents();
if (DeleteAmortization(amortControlKey))
LoadGrid();
}
break;
}
case 3:
{
msg = "Are you sure that you want to recalculate all amortization for " + amortControlKey + "?";
if (MessageBox.Show(msg, "Confirm recalculation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.DoEvents();
if (UpdateAmortization(amortControlKey))
{
MessageBox.Show("Amortization " + amortControlKey + " has been recalculated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
LoadGrid();
}
break;
}
}
}
private bool DeleteAmortization(int amortControlKey)
{
bool retval = true;
// Data objects are unmanaged code.
// Declare them above the try{} block and always dispose of them in finally{}.
SqlCommand cmd = null;
SqlTransaction trx = null;
try
{
Cursor.Current = Cursors.WaitCursor;
cmd = util.SqlConn.CreateCommand();
cmd.CommandTimeout = 600;
trx = util.SqlConn.BeginTransaction();
cmd.Transaction = trx;
cmd.CommandText = "delete from tbl_amortization where amort_control_key = " + amortControlKey;
cmd.ExecuteNonQuery();
cmd.CommandText = "update zstbl_amort_control set amort_status = 'Deleted', amort_total = 0, time_stamp = getdate() where amort_control_key = " + amortControlKey;
cmd.ExecuteNonQuery();
trx.Commit();
}
catch (Exception ex)
{
retval = false;
util.LogError(ex);
trx.Rollback();
MessageBox.Show("Delete failed: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
cmd.Dispose();
trx.Dispose();
Cursor.Current = Cursors.Default;
}
return retval;
}
private bool UpdateAmortization(int amortControlKey)
{
bool retval = true;
// Data objects are unmanaged code.
// Declare them above the try{} block and always dispose of them in finally{}.
SqlCommand cmd = null;
try
{
if (DeleteAmortization(amortControlKey))
{
cmd = util.SqlConn.CreateCommand();
Cursor.Current = Cursors.WaitCursor;
Amortizer amt = new Amortizer(cmd);
amt.AmortizeSingleMode(amortControlKey);
}
}
catch (Exception ex)
{
retval = false;
util.LogError(ex);
MessageBox.Show("Delete failed: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
cmd.Dispose();
Cursor.Current = Cursors.Default;
}
return retval;
}
private void btnDeleteAmort_Click(object sender, EventArgs e)
{
ArrayList AmortKeyList = new ArrayList();
//DataGridView dgv = (DataGridView)sender;
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in grd1.Rows)
{
if (Convert.ToBoolean(row.Cells[Del2.Name].Value) == true)
{
rows_with_checked_column.Add(row);
AmortKeyList.Add(row.Cells[colAmortKey.Name].Value);
//string importKey = grd1.Rows[grd1.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
//grd1.ClearSelection();
//if (DeleteImport(importKey))
// LoadGrid();
}
}
foreach (object obj in importKeyList)
{
int amortControlKey = (int)obj;
grd1.ClearSelection();
msg = "Are you sure that you want to delete amortization " + amortControlKey + "?";
if (MessageBox.Show(msg, "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.DoEvents();
if (DeleteAmortization(amortControlKey))
LoadGrid();
}
break;
}