在DataGridView中选择一行

时间:2011-10-28 03:07:16

标签: datagridview c#-3.0 selectionchanged

我从数据表填充DataGridView。 我使用DataGridView控件中的selectionchanged事件。

当我将新项目插入数据库并在网格中重新加载数据表时, 它默认选择datagridview中的第一行。

如何选择插入DataGridView的最后一行?

4 个答案:

答案 0 :(得分:0)

试试这个

dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;

答案 1 :(得分:0)

我为你创建了一个虚拟项目。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestApplications
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public class foo
    {
        public foo()
        {
            ID = Count + 1;
        }
        public static int Count;
        public int ID { get; set; }
        public string Name { get; set; }
    }

    List<foo> x = new List<foo>();
    foo LastSelectedItem = new foo();

    public MainWindow()
    {
        InitializeComponent();
        this.btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
        this.dgvItems.SelectionChanged += new SelectionChangedEventHandler(dgvItems_SelectionChanged);
    }

    void dgvItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(((DataGrid)sender).SelectedItem != null)
            LastSelectedItem = ((DataGrid)sender).SelectedItem as foo;
    }

    void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        if (txtItemContent.Text.Trim().Length != 0)
        {
            foo item = new foo();
            foo.Count = foo.Count + 1;
            item.Name = txtItemContent.Text;
            x.Add(item);
            dgvItems.ItemsSource = null;
            dgvItems.ItemsSource = x;
            dgvItems.SelectedItem = LastSelectedItem;
        }
    }


}
}

然后下面的代码用于XAML如果你正在使用WPF或Silverlight。(如果你使用的是winforms,请忽略它,我只是为你展示了我在代码中调用的内容)

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TestApplications.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="27"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <DataGrid x:Name="dgvItems" Grid.Row="1" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="ID" Binding="{Binding ID}" Header="ID"/>
            <DataGridTextColumn x:Name="Name" Binding="{Binding Name}" Header="Name" Width="460"/>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Orientation="Horizontal" d:LayoutOverrides="Width" HorizontalAlignment="Left">
        <Label Content="Item:" d:LayoutOverrides="Height"/>
        <TextBox x:Name="txtItemContent" TextWrapping="Wrap" VerticalAlignment="Center" MinWidth="250"/>
        <Button x:Name="btnAdd" Content="ADD" Margin="0" Width="75" VerticalAlignment="Center"/>
    </StackPanel>
</Grid>

答案 2 :(得分:0)

假设您至少有一列,请尝试设置网格的当前单元格

dataGridView1.CurrentCell = dataGridView1[0,dataGridView1.Rows.Count - 1];

答案 3 :(得分:0)

你可以尝试这样......

private void ReloadDgv(int id)
{
  // id should be the your record id 
  // This is to set forcus on change record or new record
  RefeshDgv();
  if(condition you are adding new record to datagrid view)
    dgv.Rows[dgv.Rows.Count - 1].Selected = true;

  else if (id > 0)(you are updating present record)
  {
    var i = 0;
    foreach (DataGridViewRow row in dgv.Rows)
    {
      if ((int)row.Cells[0].Value == id)
      {
        dgv.Rows[i].Selected = true;
        break;
      }
      i++;
    }
  }
}



private void RefeshDgvStaff()
{
  if (_obj.getGridViewData().DataSource != null)
  {
    dgv.DataSource = _obj.getGridViewData();  // where i am binding the datagird view ...
    dgvSetHeaders();
  }
}

你必须调用reloaddgv(id)

 public void savefunction()
 {
     // where do u insert the records into database
        reloaddgv(0);

  }