我昨天发布了类似的内容,但我认为我的措辞可能不太好,因为我通常会在几分钟内得到多个答案,但我什么也没得到。
我使用 VS 2008 在 C# 环境中工作。我可以很好地使用 datagridviews,并且可以使用按钮 (DataGridViewButtonColumn) 或复选框 (DataGridViewCheckBoxColumn) 来处理它们,没有任何问题。我已经使用按钮通过“CellContentClick”功能触发简单的操作。我已经使用复选框来检查选定的项目,然后使用datagridview外部的按钮,但仍然在表单中运行批量进程。我的代码在这篇文章的最底部。
我现在面临的任务是创建一个两者兼有的数据网格视图;所以我将有一列按钮 (DataGridViewButtonColumn)、一列复选框 (DataGridViewCheckBoxColumn),然后是 DGV 外引用复选框列的按钮。
我剩下的是,我无法弄清楚如何解决 CellContentClick,即使我单击复选框时会触发它,因为当我单击复选框时我不想触发任何内容。我通过识别它不是单击按钮的正确列,使基础代码跳过 CellContentClick 中的代码,但该复选框永远不会被选中,因为 CellContentClick 在识别框已被选中之前触发。
任何人都有任何代码片段、链接或建议。除了基础知识之外,我对 DGV 不太熟悉,但我意识到我真的需要学习。
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;
}