【问题标题】:Passing a control name? Visual Studio C# WM5传递控件名称? Visual Studio C# WM5
【发布时间】:2016-09-17 20:41:14
【问题描述】:

已经很久没有计划了。刚刚学习 VB 2008,自 1999 年以来情况发生了变化。

我有 3 个数据网格。我想将所有 3 行点击分组到一个区域并获取 CurrentRowIndex。

那么你可以通过一个控件名称来获取3个网格的CurrentRowIndex吗?

我知道这不起作用:

private void tblCollectionDataGrid_Click(object sender, EventArgs e)
{
    Control control = (Control)sender;
    MessageBox.Show(control.Name);

    row = control.Name.CurrentRowIndex;
}

或者你必须把每一个都打出来? 这可行,但......如果可能的话,希望它更简单。

private void tblCollectionDataGrid_Click(object sender, EventArgs e)
{
    Control control = (Control)sender;
    MessageBox.Show(control.Name);

    switch (control.Name)
    { 
        case "tblMemoDataGrid":
            row = tblMemoDataGrid.CurrentRowIndex;
            break;
        case "tblReportDataGrid":
            row = tblReportDataGrid.CurrentRowIndex;
            break;
        case "tblInfoDataGrid":
            row = tblInfoDataGrid.CurrentRowIndex;
            break;
    }
}

【问题讨论】:

    标签: c# datagrid windows-mobile-5.0


    【解决方案1】:

    我不确定你的意思

    "将所有 3 行点击分组到一个区域并获取 CurrentRowIndex"

    如果有 3 个 DataGridView,则可能会有 3 个不同的行索引。

    对所有 3 个 DatagridView 使用相同的“单击”事件

    private void dataGridView_Click(object sender, EventArgs e)
    {
      int index1 = dataGridView1.CurrentCell.RowIndex;
      int index2 = dataGridView2.CurrentCell.RowIndex;
      int index3 = dataGridView3.CurrentCell.RowIndex;
      MessageBox.Show("i1: " + index1 + "  i2: " + index2 + "  i3: " + index3);
    }
    

    这将允许您查看为所有 DatagridViews 选择了哪些行。 也许我不明白这个问题....

    【讨论】:

    • 类似于:codeforeach(控制 this.Controls 中的控件){ if(控件为 TextBox){ TextBox textBox =(控件为 TextBox);文本框.Text = ""; } } code 而不是文本框,它将是 ... row = control.CurrentRowIndex;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2017-07-11
    • 2018-07-07
    相关资源
    最近更新 更多