DataGridView行计数0后的事件处理程序

本文关键字:事件处理 程序 DataGridView | 更新日期: 2023-09-27 18:11:46

我遇到了一个非常奇怪的问题动态构建一个DataGridView和一组单选按钮。我有一组单选按钮和面板内的DataGridView。我遇到的问题是我试图在一些单选按钮上使用事件处理程序来获得DataGridView中的行数。我使用的数据表有3行。您可以手动向DGV添加行来重现这个问题,这无关紧要。当我调用行时。在我初始化DataGridView后,它按计划工作并返回3,但当EventHandler被触发时,返回的行数为0。我的代码如下:

    private DataSet testData;
    private DataTable testDataTable;
    public Form1(API _api)
    {
        InitializeComponent();
        api = _api;
        businessLayer = new BusinessLayer();
        testData = api.getDataSet();
        testDataTable = businessLayer.getDataTable(testData);
        buildDataGridView();
    }
    // Build the DataGridView
    private void buildDataGridView()
    {
        int numOfRows = testDataTable.Rows.Count;
        DataGridView testDataGridView = new DataGridView();
        testDataGridView.Columns.Add("Description", "");
        testDataGridView.Columns.Add("Name", "");
        testDataGridView.Rows.Add(numOfRows);
        testDataGridView.Location = new Point(150, 20);
        for (int i = 0; i < numOfRows; i++)
        {
            RadioButton rbutton = new RadioButton();
            rbutton.Name = testDataTable.Rows[i].Field<string>("Name").ToString() + "RadioButton";
            rbutton.Text = testDataTable.Rows[i].Field<string>("Name");
            rbutton.Tag = testDataTable.Rows[i].Field<string>("SortOrder");
            rbutton.CheckedChanged += rbutton_CheckedChanged;
            if (i == 0)
            {
                rbutton.Checked = true;
            }
            rbutton.Location = new Point(5, 20 * (i + 1));
            testPanel.Controls.Add(definition);
        }
        testPanel.Controls.Add(testDataGridView);
        textBox1.Text = testDataGridView.Rows.Count.ToString();
    }
    // RadioButton CheckChanged EventHandler
    private void definition_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rb = (RadioButton)sender;
        if(rb.Checked)
        {
            textBox1.Text = testDataGridView.Rows.Count.ToString();
        }
    }

这真的把我弄糊涂了,但如果我弄明白了,我一定会学到一些东西。我不知道这是否与DGV和单选按钮在面板内有关,我不知道……谢谢你的帮助和帮助我学习!

DataGridView行计数0后的事件处理程序

Grant Winney在评论中解释了buildDataGridView()中引用的DataGridView和definition_CheckedChanged()中引用的DataGridView是不一样的…一个内存堆和内存堆栈的例子。下面是我试图做的一个更简单的例子:

public partial class TestingForm2 : Form
{
    private DataTable testDataTable;
    // Must Declare here so the value is stored in heap memory
    // so other functions can access and use the value
    private DataGridView testDataGridView;
    public TestingForm2()
    {
        InitializeComponent();
        buildDataTable();
        buildDataGridView();
        buildRadioButtons();
    }
    private void buildRadioButtons()
    {
        int numOfRadioButtons = testDataTable.Rows.Count;
        for(int i = 0; i < numOfRadioButtons; i++)
        {
            RadioButton rb = new RadioButton();
            rb.Name = testDataTable.Rows[i].Field<string>("Name") + "RadioButton";
            rb.Text = testDataTable.Rows[i].Field<string>("Name");
            rb.Tag = testDataTable.Rows[i].Field<string>("Number");
            if(i == 0)
            {
                rb.Checked = true;
            }
            rb.Location = new Point(5, 20 * (i + 1));
            rb.CheckedChanged += rb_CheckedChanged;
            panel1.Controls.Add(rb);
        }
    }
    private void buildDataGridView()
    {
        // Get number of rows in testDataTable
        int numOfRows = testDataTable.Rows.Count;
        // Build the empty DGV
        // If I were to declare the DGV here the value given would have
        // been stored in the memory stack and could only be accessed by
        // this function.  This is where I went wrong and made a silly mistake
        testDataGridView = new DataGridView();
        testDataGridView.Columns.Add("Name", "");
        testDataGridView.Columns.Add("Number", "");
        testDataGridView.Rows.Add(numOfRows);
        testDataGridView.Location = new Point(150, 20);
        testDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        testDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        testDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
        testDataGridView.AllowUserToAddRows = false;
        testDataGridView.AllowUserToDeleteRows = false;
        testDataGridView.MultiSelect = false;
        testDataGridView.ColumnHeadersVisible = false;
        testDataGridView.RowHeadersVisible = false;
        panel1.Controls.Add(testDataGridView);
    }
    private void buildDataTable()
    {
        testDataTable = new DataTable();
        // Add the Columns
        testDataTable.Columns.Add("Name");
        testDataTable.Columns.Add("Number");
        // Create and add the rows
        for (int i = 0; i < 3; i++)
        {
            // Create the new row
            DataRow dr;
            object[] rowItems = new object[testDataTable.Columns.Count];
            if (i == 0)
            {
                rowItems[0] = "Bob";
                rowItems[1] = "1";
            }
            else if (i == 1)
            {
                rowItems[0] = "John";
                rowItems[1] = "2";
            }
            else
            {
                rowItems[0] = "George";
                rowItems[1] = "3";
            }
            // Add the row
            dr = testDataTable.NewRow();
            dr.ItemArray = rowItems;
            testDataTable.Rows.Add(dr);
        }
    }
    private void TestingForm2_Load(object sender, EventArgs e)
    {
        textBox1.Text = testDataGridView.Rows.Count.ToString();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = testDataGridView.Rows.Count.ToString();
    }
    private void rb_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rb = (RadioButton)sender;
        int index = int.Parse(rb.Tag.ToString()) - 1;
        if (rb.Checked)
        {
            testDataGridView.Rows[index].Selected = true;
        }
    }
}

很抱歉大家的错误!谢谢你的帮助!