0

私は昨日このようなものを投稿しましたが、私は通常数分以内に複数の回答を受け取り、何も得られなかったので、私の言葉遣いはうまくいかなかったと思います。

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;
            }
4

0 に答える 0