下面是一个示例,我使用DatagridView在列表(类对象列表)上执行插入更新和删除
PersonState
)。
DatagridViews数据源需要包含一个DataTable,为了弥补这个差距,我使用了一个名为
ConvertToDatatable()
。
作为我的出发点,我从另一个网站上建议的一个项目开始
link
作者:Anup Kumar Sharma。
使用以下代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace InsertUpdateDelete {
public partial class Form1 : Form {
public class PersonState {
public string Name { get; set; }
public string State { get; set; }
}
public List<PersonState> listOfPersonState;
public Form1() {
InitializeComponent();
listOfPersonState = new List<PersonState>();
}
//Display Data in DataGridView
private void DisplayData() {
DataTable dt = new DataTable();
dt = ConvertToDatatable();
dataGridView1.DataSource = dt;
}
//Clear Data
private void ClearData() {
txt_Name.Text = "";
txt_State.Text = "";
}
public DataTable ConvertToDatatable() {
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("State");
foreach (var item in listOfPersonState) {
var row = dt.NewRow();
row["Name"] = item.Name;
row["State"] = item.State;
dt.Rows.Add(row);
}
return dt;
}
private void AddToList(string text1, string text2) {
listOfPersonState.Add(new PersonState { Name = text1, State = text2 });
}
private void UpdateToList(string text1, string text2) {
int index = dataGridView1.SelectedRows[0].Index;
listOfPersonState[index] = new PersonState { Name = text1, State = text2 };
}
private void DeleteToList() {
int index = dataGridView1.SelectedRows[0].Index;
listOfPersonState.RemoveAt(index);
}
private void btn_Insert_Click(object sender, EventArgs e) {
if (txt_Name.Text != "" && txt_State.Text != "") {
AddToList(txt_Name.Text, txt_State.Text);
//MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
} else {
MessageBox.Show("Please Provide Details!");
}
}
private void btn_Update_Click(object sender, EventArgs e) {
if (txt_Name.Text != "" && txt_State.Text != "") {
if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0) {
UpdateToList(txt_Name.Text, txt_State.Text);
//MessageBox.Show("Record Updated Successfully");
DisplayData();
ClearData();
}
} else {
MessageBox.Show("Please Select Record to Update");
}
}
private void btn_Delete_Click(object sender, EventArgs e) {
if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0) {
DeleteToList();
//MessageBox.Show("Record Deleted Successfully!");
DisplayData();
ClearData();
} else {
MessageBox.Show("Please Select Record to Delete");
}
}
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
FillInputControls(e.RowIndex);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
FillInputControls(e.RowIndex);
}
private void FillInputControls(int Index) {
if (Index > -1) {
txt_Name.Text = dataGridView1.Rows[Index].Cells[0].Value.ToString();
txt_State.Text = dataGridView1.Rows[Index].Cells[1].Value.ToString();
}
}
}
}
学习内容:
-
请注意,我在类中使用了属性。
而不是使用:
public class PersonState {
public string Name;
public string State;
}
我使用过:
public class PersonState {
public string Name { get; set; }
public string State { get; set; }
}
出于某种原因,在试图认同价值观时,前者不起作用。
-
我已将对象列表作为类变量,以便所有
函数可以直接访问该列表,而不是传递该列表
作为参数。
public List<PersonState> listOfPersonState;
-
我将插入、更新和删除的逻辑替换为DB,插入
更新并删除到列表。
private void AddToList(string text1, string text2) {
listOfPersonState.Add(new PersonState { Name = text1, State = text2 });
}
private void UpdateToList(string text1, string text2) {
int index = dataGridView1.SelectedRows[0].Index;
listOfPersonState[index] = new PersonState { Name = text1, State = text2 };
}
private void DeleteToList() {
int index = dataGridView1.SelectedRows[0].Index;
listOfPersonState.RemoveAt(index);
}
注意:我直接从列表中分配网格,因此我的网格和列表始终具有相同的索引,因为我在插入、更新和删除按钮操作时使用显示功能来确保这一点。
private void DisplayData() {
DataTable dt = new DataTable();
dt = ConvertToDatatable();
dataGridView1.DataSource = dt;
}
public DataTable ConvertToDatatable() {
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("State");
foreach (var item in listOfPersonState) {
var row = dt.NewRow();
row["Name"] = item.Name;
row["State"] = item.State;
dt.Rows.Add(row);
}
return dt;
}