0

我在动态构建 DataGridView 和一组单选按钮时遇到了一个非常奇怪的问题。我在面板中有一组单选按钮和一个 DataGridView。我遇到的问题是我正在尝试使用某些单选按钮上的事件处理程序来获取 DataGridView 中的行数。我使用的 DataTable 有 3 行。您可以重新创建此问题,手动将行添加到 DGV,这没关系。当我在初始化 DataGridView 后调用 rows.count 时,它按计划工作并返回 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 和单选按钮位于面板内的事实有关,我不知道...感谢您对此提供的任何帮助并帮助我学习!

4

1 回答 1

0

正如格兰特温尼在评论中解释的那样,在 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;
        }

    }
}

对大家的错误深表歉意!谢谢您的帮助!

于 2014-07-08T15:50:20.870 回答