简体   繁体   English

我不想在单击带有 DataGridView 的复选框时添加新行。(C#)

[英]I don't want to add the new row when I clicked a checkbox with DataGridView.(C#)

I'm trying to use dataGridView(with C#), and I have a question.我正在尝试使用 dataGridView(通过 C#),但我有一个问题。

在此处输入图像描述

This is my dataGridView.这是我的数据网格视图。

If i click on the checkbox, which is shown in red squares, a new row is automatically added.如果我单击以红色方块显示的复选框,则会自动添加一个新行。 Why is this?为什么是这样?

In the code, I have never added any event when I clicked the checkbox.在代码中,我单击复选框时从未添加任何事件。

code) namespace TestWinForm { public partial class Form1: Form { List saved_file_names = new List(); code) namespace TestWinForm { public partial class Form1: Form { List saved_file_names = new List(); int table_index = 0; int table_index = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void add_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "열기";
            ofd.Filter = "txt파일 | *.txt";
            ofd.Multiselect = true; //  파일 다중 선택

            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                foreach (string file_name in ofd.FileNames)
                {
                    //  1. 중복체크
                    if (saved_file_names.Contains(file_name))
                        continue;

                    //  2. 중복되지않은 파일들을 추가.
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[table_index].Cells[1].Value = table_index + 1;
                    dataGridView1.Rows[table_index].Cells[2].Value = file_name;
                    saved_file_names.Add(file_name);
                    dataGridView1.Rows[table_index].Cells[3].Value = "none";
                    table_index++;
                }
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            bool is_checked = false;
            List<int> delete_index = new List<int>();
            for (int i = 0; i < table_index; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                    delete_index.Add(i);
            }

            if (delete_index.Count == 0)
                return;

            delete_index.Reverse();

            foreach (var index in delete_index)
            {
                table_index--;
                saved_file_names.RemoveAt(index);
                dataGridView1.Rows.RemoveAt(index);
            }


        }

        private void decode_Click(object sender, EventArgs e)
        {

        }
        private void ok_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex == -1)
            {
                e.PaintBackground(e.ClipBounds, false);
                Point pt = e.CellBounds.Location;

                int nChkBoxWidth = 15;
                int nChkBoxHeight = 15;
                int offsetx = (e.CellBounds.Width - nChkBoxWidth) / 2;
                int offsety = (e.CellBounds.Height - nChkBoxHeight) / 2;

                pt.X += offsetx;
                pt.Y += offsety;

                CheckBox cb = new CheckBox();

                cb.Size = new Size(nChkBoxWidth, nChkBoxHeight);
                cb.Location = pt;
                cb.Checked = true;
                cb.CheckedChanged += new EventHandler(dgvCheckBox_CheckedChanged);

                ((DataGridView)sender).Controls.Add(cb);

                e.Handled = true;

            }

        }

        private void dgvCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                r.Cells[0].Value = ((CheckBox)sender).Checked;
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在 C# 中将 CheckBox 值添加到没有数据库的 DataGridView 中 - I want to add CheckBox Value into DataGridView without Database in C# 当我在C#Windows应用程序中选中复选框时,如何更改datagridview行中的颜色? - how to change the color in datagridview row when i check the checkbox in C# windows applications? 编辑datagridview。 Visual C#2008 - Editing datagridview. Visual C# 2008 我想以编程方式在 C# 中生成对 DataGridView 行的点击 - I want to programmatically generate a click on a DataGridView Row in C# C#我想使用循环语句在Datagridview中添加Value数据 - C# I want to add the Value data in datagridview with looping statement 我想在现有的复选框gridview表中添加新的复选框行,也想在数据库中添加该新的复选框行 - I want to add new checkbox rows in existing checkbox gridview table and also want to add that new checkbox row in database how will i do it C#DataGridView添加一行 - 验证时,如何知道该行是新的还是尚未提交? - C# DataGridView Adding a Row - When validating, how do I know the row is new and not yet committed? 将新行添加到已排序的datagridview C# - Add new row to sorted datagridview c# 将新行添加到DataGridView C# - Add new row to DataGridView C# C# - 向DataGridView添加新行 - C# - Add new row to a DataGridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM