DataGridView da bir qatorni tanlash

DataGridViewni ma'lumotlardan to'ldiraman. DataGridView tekshiruvidagi ixtisoslashtirilgan tadbirni ishlataman.

Ma'lumotlar bazasiga yangi ma'lumotlar kiritish va panjarada ma'lumotni qayta yuklayotganimda, sukut bo'yicha datagridview ichida birinchi qatorni tanlaydi.

DataGridView-ga kiritilgan oxirgi qatorni qanday tanlash mumkin?

0

4 javoblar

bu kabi harakat qilib ko'rishingiz mumkin ....

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) ga qo'ng'iroq qilishingiz kerak

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

  }
0
qo'shib qo'ydi

Buni ko'ring

dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
0
qo'shib qo'ydi
Oldindan ishlatilganman.
qo'shib qo'ydi muallif Mohamed Reheem, manba
u ishlamayapti?
qo'shib qo'ydi muallif Anuraj, manba

Siz uchun qo'g'irchoqli loyiha yaratdim.

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
{
/// 
/// Interaction logic for MainWindow.xaml
/// 
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 x = new List();
    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;
        }
    }


}
}

Keyin XAML uchun quyidagi kodni kiriting wpf yoki Silverlight foydalanayotgan bo'lsangiz (Winformslardan foydalanmasangiz, men uni siz uchun kodni qanday chaqirayotganim haqidagi fikrni ko'rsatganman)



    
        
        
    
    
        
            
            
        
    
    
        

0
qo'shib qo'ydi

Sizning ustki ustuningiz bor deb hisoblasangiz, gridning joriy versiyasini o'rnatishga harakat qilasiz

dataGridView1.CurrentCell = dataGridView1[0,dataGridView1.Rows.Count - 1];
0
qo'shib qo'ydi