通过显示记录号来浏览数据网格视图

时间:2015-05-10 16:31:44

标签: c# sql-server datagridview

任何人都可以帮助我吗?我想浏览一个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;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用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
}