【问题标题】:Navigate through a datagridview by showing record number通过显示记录号浏览数据网格视图
【发布时间】:2015-05-10 16:31:43
【问题描述】:

谁能帮帮我?我想在 datagridview 中导航 - 当我从 datagridview 中选择一条记录时,我想知道它的记录号,例如记录 2of3。

private void labelUpdate()
{
    if (hwdg.SelectedRows.Count >= inc)
    {
        hwdg.FirstDisplayedScrollingRowIndex = inc;
        hwdg.Rows[inc].Selected = true;
       //inc++;
        label27.Text = "Record" + (inc + 1) + " of " + ds2.Tables["tblComp"].Rows.Count;
    }
    else if(hwdg.SelectedRows.Count <= inc)
    {
        hwdg.Rows[inc - 1].Selected = true;
        label27.Text = "Record" + (inc + 1) + " of " + ds2.Tables["tblComp"].Rows.Count;
    }
}

【问题讨论】:

  • inc 来自哪里?我们还需要有关您的预期与实际行为的更多详细信息。

标签: c# sql-server datagridview


【解决方案1】:

您可以只使用hwdg.Rows[inc]["Id"],因为您有所选行的索引 - 其中“Id”是您的数据源中 ID 列的名称。

即:

private void labelUpdate()
{
    int id;
    if (hwdg.SelectedRows.Count >= inc)
    {
        hwdg.FirstDisplayedScrollingRowIndex = inc;
        hwdg.Rows[inc].Selected = true;
        id = (int)hwdg.Rows[inc]["Id"];
       //inc++;
        label27.Text = "Record" + (inc + 1) + " of " + ds2.Tables["tblComp"].Rows.Count;
    }
    else if(hwdg.SelectedRows.Count <= inc)
    {
        hwdg.Rows[inc - 1].Selected = true;
        id = (int)hwdg.Rows[inc - 1]["Id"];
        label27.Text = "Record" + (inc + 1) + " of " + ds2.Tables["tblComp"].Rows.Count;
    }
    // Do whatever you want to do with the id
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多