WinForm中DataGridView添加,刪除,修改操作具體方法
更新時間:2013年10月28日 15:36:48 作者:
這篇文章介紹了WinForm中DataGridView添加,刪除,修改操作具體方法,有需要的朋友可以參考一下
1.添加操作,代碼如下:
復(fù)制代碼 代碼如下:
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實體類代碼如下:
復(fù)制代碼 代碼如下:
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.修改操作,代碼如下:
其實思路很簡單,就是點擊行的時候,獲取行內(nèi)數(shù)據(jù)信息,然后填充到控件內(nèi),修改后,點擊‘修改配置'后即可保存修改。
所以首先設(shè)置點擊控件的時候,是選擇一行,如圖:
在CellClick事件中完成,當(dāng)點擊行的時候,將行數(shù)據(jù)填充到控件內(nèi),代碼如下:
復(fù)制代碼 代碼如下:
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);
}
點擊修改按鈕內(nèi)代碼如下:
復(fù)制代碼 代碼如下:
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.刪除操作,代碼如下:
復(fù)制代碼 代碼如下:
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實現(xiàn)類似Excel表格的查找替換功能
- C#開發(fā)WinForm之DataGridView開發(fā)詳解
- Winform讓DataGridView左側(cè)顯示圖片
- Winform在DataGridView中顯示圖片
- WinForm中DataGridView折疊控件【超好看】
- winform用datagridview制作課程表實例
- WinForm DataGridView控件隔行變色的小例子
- C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
相關(guān)文章
如何在Mac系統(tǒng)使用Visual Studio Code運行Python
這篇文章主要介紹了Mac使用Visual Studio Code運行Python環(huán)境的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Ruby創(chuàng)建數(shù)組方法總結(jié)
在本篇文章里小編給大家分享了關(guān)于Ruby創(chuàng)建數(shù)組方法的知識點內(nèi)容,對戲有興趣的朋友們學(xué)習(xí)下。2019-01-01