WinForm中DataGridView添加,刪除,修改操作具體方法
1.添加操作,代碼如下:
IList<SelfRun> selfRunConfigs = new List<SelfRun>();
private void btnNewConfig_Click(object sender, EventArgs e)
{
try
{
string _lampNo = UpDownSelfLampNo.Value.ToString();
int _ctrlGpNo = Convert.ToInt16(UpDownCtrlGpCnt.Value);
string _opWay = string.Format("{0}", rbConfig.Checked == true ? 1 : 0);
string _opCtuch = GetSelectedCtuCh();
if (CheckNewConfigIsLega(_ctrlGpNo, _opCtuch))
{
string _opType = rbCgOpen.Checked == true ? "01" : rbCgClose.Checked == true ? "00" : "02";
selfRunConfigs.Add(new SelfRun(_opCtuch, _opType, Convert.ToInt32(UpDownTime.Value)));
}
BindGridViewForIList<SelfRun>(gcConfigShow, selfRunConfigs);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("新增配置失敗,原因:{0}", ex.Message.Trim()));
}
}
private void BindGridViewForIList<T>(DataGridView gv, IList<T> datasource)
{
BindingList<T> _bindinglist = new BindingList<T>(datasource);
BindingSource _source = new BindingSource(_bindinglist, null);
gv.DataSource = _source;
}
SelfRun實(shí)體類代碼如下:
public struct SelfRun
{
public SelfRun(string _opCtuCh, string _opWay, int _opTime)
: this()
{
OpCtuCh = _opCtuCh;
OpWay = _opWay;
OpTime = _opTime;
}
public string OpCtuCh
{
get;
set;
}
public string OpWay { get; set; }
public int OpTime { get; set; }
}
界面綁定,如圖:

效果如圖:

2.修改操作,代碼如下:
其實(shí)思路很簡(jiǎn)單,就是點(diǎn)擊行的時(shí)候,獲取行內(nèi)數(shù)據(jù)信息,然后填充到控件內(nèi),修改后,點(diǎn)擊‘修改配置'后即可保存修改。
所以首先設(shè)置點(diǎn)擊控件的時(shí)候,是選擇一行,如圖:

在CellClick事件中完成,當(dāng)點(diǎn)擊行的時(shí)候,將行數(shù)據(jù)填充到控件內(nèi),代碼如下:
private void gcConfigShow_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) return;
DataGridView _dgv = (DataGridView)sender;
string _opWay = _dgv.Rows[e.RowIndex].Cells["OpWay"].Value.ToString();
switch (_opWay)
{
case "00":
ThreadSafeOpRadioButton(rbCgClose, true);
break;
case "01":
ThreadSafeOpRadioButton(rbCgOpen, true);
break;
case "02":
ThreadSafeOpRadioButton(rbSaveOne, true);
break;
}
string _opCtuch = _dgv.Rows[e.RowIndex].Cells["OpCtuCh"].Value.ToString();
for (int i = 0; i < _opCtuch.Length; i++)
{
if (i == 0)
ThreadSafeCheckBox(ckch1, _opCtuch[i].Equals('1'));
if (i == 1)
ThreadSafeCheckBox(ckch2, _opCtuch[i].Equals('1'));
if (i == 2)
ThreadSafeCheckBox(ckch3, _opCtuch[i].Equals('1'));
if (i == 3)
ThreadSafeCheckBox(ckch4, _opCtuch[i].Equals('1'));
if (i == 4)
ThreadSafeCheckBox(ckch5, _opCtuch[i].Equals('1'));
if (i == 5)
ThreadSafeCheckBox(ckch6, _opCtuch[i].Equals('1'));
if (i == 6)
ThreadSafeCheckBox(ckch7, _opCtuch[i].Equals('1'));
if (i == 7)
ThreadSafeCheckBox(ckch8, _opCtuch[i].Equals('1'));
}
string _opTime = _dgv.Rows[e.RowIndex].Cells["OpTime"].Value.ToString();
decimal _time;
if (decimal.TryParse(_opTime, out _time))
ThreadSfeOpUpDown(UpDownTime, _time);
}
點(diǎn)擊修改按鈕內(nèi)代碼如下:
private void btnUpdateConfig_Click(object sender, EventArgs e)
{
try
{
if (CheckSelectedRow())
{
int _rowIndex = gcConfigShow.CurrentCell.RowIndex;
int _ctrlGpNo = Convert.ToInt16(UpDownCtrlGpCnt.Value);
string _opWay = string.Format("{0}", rbConfig.Checked == true ? 1 : 0);
string _opCtuch = GetSelectedCtuCh();
string _opType = rbCgOpen.Checked == true ? "01" : rbCgClose.Checked == true ? "00" : "02";
SelfRun _selfRunByRowIndex = selfRunConfigs[_rowIndex];
_selfRunByRowIndex.OpCtuCh = GetSelectedCtuCh();
_selfRunByRowIndex.OpTime = Convert.ToInt32(UpDownTime.Value);
_selfRunByRowIndex.OpWay = _opType;
selfRunConfigs.RemoveAt(_rowIndex);
selfRunConfigs.Add(_selfRunByRowIndex);
BindGridViewForIList<SelfRun>(gcConfigShow, selfRunConfigs);
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("修改配置失敗,原因:{0}", ex.Message.Trim()));
}
}
3.刪除操作,代碼如下:
private void btnDeleteConfig_Click(object sender, EventArgs e)
{
if (CheckSelectedRow())
{
if (MessageBox.Show("是否刪除該行數(shù)據(jù)?", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
selfRunConfigs.RemoveAt(gcConfigShow.CurrentCell.RowIndex);
BindGridViewForIList<SelfRun>(gcConfigShow, selfRunConfigs);
}
}
}
- C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色
- WinForm使用DataGridView實(shí)現(xiàn)類似Excel表格的查找替換功能
- C#開發(fā)WinForm之DataGridView開發(fā)詳解
- Winform讓DataGridView左側(cè)顯示圖片
- Winform在DataGridView中顯示圖片
- WinForm中DataGridView折疊控件【超好看】
- winform用datagridview制作課程表實(shí)例
- WinForm DataGridView控件隔行變色的小例子
- C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
相關(guān)文章
C#判斷字符串是否存在字母及字符串中字符的替換實(shí)例
這篇文章主要介紹了C#判斷字符串是否存在字母及字符串中字符的替換,實(shí)例形式講述了C#針對(duì)字符串的正則操作,需要的朋友可以參考下2014-10-10
C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
C#的通用DbHelper類(支持?jǐn)?shù)據(jù)連接池)示例詳解
這篇文章主要介紹了C#的通用DbHelper類支持?jǐn)?shù)據(jù)連接池,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C#中緩存System.Web.Caching用法總結(jié)
本文詳細(xì)講解了C#中緩存System.Web.Caching的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#中的只讀結(jié)構(gòu)體(readonly struct)詳解
這篇文章主要給大家介紹了關(guān)于C#中只讀結(jié)構(gòu)體(readonly struct)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

