DataGridViews与DataGridViewCheckBoxColumn和DataGridViewButtonColumn

时间:2012-10-25 19:19:02

标签: c# datagridview datagridviewcheckboxcell

我昨天发布了这样的内容,但想想也许我的措辞不是很好,因为我通常会在几分钟内得到多个答案而且什么都没有。

我使用VS 2008在C#环境中工作。我使用datagridviews,并且可以使用按钮(DataGridViewButtonColumn)或复选框(DataGridViewCheckBoxColumn)来处理它们而没有任何问题。我已经使用按钮通过“CellContentClick”功能触发简单操作。我已经使用复选框检查所选项目,然后使用外部数据网格视图按钮,但仍然在表单中运行批量处理。我的代码就在这篇文章的最底部。

我现在面临着创建具有两者的datagridview的任务;所以我将有一列按钮(DataGridViewButtonColumn),一列复选框(DataGridViewCheckBoxColumn),然后是一个按钮otuside引用复选框列的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;
            }

0 个答案:

没有答案