我有一个DataGridView
,只要按下ENTER键,我就会从一个单元格移动到另一个单元格。
我已设法找到单元格处于编辑模式而不处于编辑模式的解决方案。
我面临的问题是当用户在最后一行时,当他正在编辑一个单元格然后按下ENTER时,一个事件没有触发(selectionChanged
)并且单元格没有离开焦点。它只是从编辑模式退出。这很奇怪,因为它只发生在最后一行。所有其他行都按预期工作。
我正在使用2个事件,以便在单元格处于编辑模式时移动到Enter键上的下一个单元格。
private void dgvTT_SelectionChanged(object sender, EventArgs e)
{
if (MouseButtons != 0) return;
if (_celWasEndEdit != null && dgvTT.CurrentCell != null)
{
// if we are currently in the next line of last edit cell
if (dgvTT.CurrentCell.RowIndex == _celWasEndEdit.RowIndex + 1 &&
dgvTT.CurrentCell.ColumnIndex == _celWasEndEdit.ColumnIndex)
{
int iColNew;
int iRowNew = 0;
if (_celWasEndEdit.ColumnIndex >= colMaam.Index)
{
iColNew = colMisparHeshbon.Index;
iRowNew = dgvTT.CurrentCell.RowIndex;
}
else
{
iColNew = dgvTT.Columns.GetNextColumn(_celWasEndEdit.OwningColumn, DataGridViewElementStates.Displayed, DataGridViewElementStates.None).Index;
iRowNew = _celWasEndEdit.RowIndex;
}
dgvTT.CurrentCell = dgvTT[iColNew, iRowNew];
}
}
_celWasEndEdit = null;
}
private void dgvTT_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
_celWasEndEdit = dgvTT[e.ColumnIndex, e.RowIndex];
}
有什么建议吗?
答案 0 :(得分:1)
import UIKit
import RealmSwift
class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
@IBOutlet weak var ttableview: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array1.count
}
func ttableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array1.count
}
/////
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ttableview.dequeueReusableCell(withIdentifier: "cell") as! Cell
cell.lable1.text = array1[indexPath.row]
cell.lable2.text = array2[indexPath.row]
return cell
}
/////
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
//this delet Realm
let cat1 = array1[indexPath.row]
let cat2 = array2[indexPath.row]
let realm = try! Realm()
try realm.write {
realm.delete(cat1)
realm.delete(cat2)
}
//this delete table view row and array
array1.remove(at: indexPath.row)
array2.remove(at: indexPath.row)
ttableview.deleteRows(at: [indexPath], with: .fade)
}
}
var array1 = [String]()
var array2 = [String]()
///////////////////
//add 2 text filed in tableview
@IBAction func Add(_ sender: Any) {
addCat()
array1.insert(text1.text!, at: 0)
array2.insert(text2.text!, at: 0)
self.ttableview.reloadData()
}
/////////////
func addCat(){
let realm = try! Realm()
let mike = CCat()
mike.name = (text1.text!)
mike.job = (text2.text!)
try! realm.write {
realm.add(mike)
//print(" \(mike.name) and \(mike.job)")
}
}
////////////////
func queryPeople(){
let realm = try! Realm()
let allPeople = realm.objects(CCat.self)
// var byname = allPeople.sorted(byProperty: "name", ascending: false)
for person in allPeople {
array1.insert(person.name, at: 0)
array2.insert(person.job, at: 0)
print("\(person.name) to \(person.job)")
ttableview.reloadData()
}
}
///////////////////
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ttableview.delegate = self
ttableview.dataSource = self
// addCat()
queryPeople()
}