window form 练习使用datagridview
来源:互联网 发布:美国制造业回流 知乎 编辑:程序博客网 时间:2024/06/06 07:50
为了练习DGV做了一个简单的通讯录, DGV的name是cfDGVRecord,有个toolscirpt 加了一个textbox做查询输入,再有一个按钮做查询后的返回原来的所有记录。
没有默认的数据源绑定,也没有进行太多的操作,添加,修改,删除,查询几个简单的操作,由于第一次练习,c#也刚接触,可能有很多不足,望指正。
private void ContentForm_Load(object sender, EventArgs e)
{
PopulateDataGridView();
}
private void PopulateDataGridView()
{
record aLR = new record();
cfDGVRecord.DataSource = aLR.GetAllRecordByUsername(this.currentUserName);
cfDGVRecord.DataMember = aLR.dbName;
cfDGVRecord.Columns["RecordID"].Visible = false;
cfDGVRecord.Columns["UserName"].Visible = false;
currentRecordCount = aLR.GetAllRecordCountByUsername(currentUserName);
contentFeedBack.Text = "记录共 " + currentRecordCount.ToString() + " 条";
}
//某个单元格编辑结束的时候
private void cfDGVRecord_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int currentRowIndex = e.RowIndex;
int currentColumnIndex = e.ColumnIndex;
string recordID = "";
try
{
recordID = cfDGVRecord.Rows[currentRowIndex].Cells["RecordID"].Value.ToString();
DataGridView dgv = (DataGridView)sender;
DataGridViewCell dgvC = dgv[currentColumnIndex, currentRowIndex];
string currentColumnName = dgv.Columns[currentColumnIndex].Name;
string newCellValue = dgvC.Value.ToString();
if (recordID.Length > 0)
{
record cfRecord = new record();
cfRecord.UpdateByFieldnameFieldValueRecordID(currentColumnName, newCellValue, recordID);
}
}
catch
{
recordID = "";
}
}
private void cfDGVRecord_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
DialogResult response = MessageBox.Show("确定删除这个记录?不可恢复", "确定删除这个记录?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (response == DialogResult.Yes)
{
int currentRowIndex = e.Row.Index;
string recordID = "";
try
{
recordID = cfDGVRecord.Rows[currentRowIndex].Cells["RecordID"].Value.ToString();
record cfRecord = new record();
cfRecord.DeleteRecordByRecordID(recordID);
currentRecordCount--;
contentFeedBack.Text = "记录共 " + currentRecordCount.ToString() + " 条";
}
catch
{ }
}
else
{
e.Cancel = true;
}
}
private void cfDGVRecord_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
int currentRowIndex = e.Row.Index-1;
string tempRecordID = "";
try
{
tempRecordID = cfDGVRecord.Rows[currentRowIndex-1].Cells["RecordID"].Value.ToString();
}
catch
{
tempRecordID = "";
}
if ((currentRowIndex > 0 && tempRecordID.Length > 0) ||currentRowIndex == 0)
{
try
{
string name = cfDGVRecord.Rows[currentRowIndex].Cells["Name"].Value.ToString();
string nickname = cfDGVRecord.Rows[currentRowIndex].Cells["NickName"].Value.ToString();
string sex = cfDGVRecord.Rows[currentRowIndex].Cells["Sex"].Value.ToString();
string birth = cfDGVRecord.Rows[currentRowIndex].Cells["Birthday"].Value.ToString();
string mobile = cfDGVRecord.Rows[currentRowIndex].Cells["Mobile"].Value.ToString();
string officetel = cfDGVRecord.Rows[currentRowIndex].Cells["OfficeTel"].Value.ToString();
string hometel = cfDGVRecord.Rows[currentRowIndex].Cells["HomeTel"].Value.ToString();
string department = cfDGVRecord.Rows[currentRowIndex].Cells["Department"].Value.ToString();
string positon = cfDGVRecord.Rows[currentRowIndex].Cells["Position"].Value.ToString();
string address = cfDGVRecord.Rows[currentRowIndex].Cells["Address"].Value.ToString();
string email = cfDGVRecord.Rows[currentRowIndex].Cells["Email"].Value.ToString();
if (name.Trim().Length == 0)
{
name = "-";
}
if (nickname.Trim().Length == 0)
{
nickname = "-";
}
if (sex.Trim().Length == 0)
{
sex = "-";
}
if (birth.Trim().Length == 0)
{
birth = "-";
}
if (mobile.Trim().Length == 0)
{
mobile = "-";
}
if (officetel.Trim().Length == 0)
{
officetel = "-";
}
if (hometel.Trim().Length == 0)
{
hometel = "-";
}
if (department.Trim().Length == 0)
{
department = "-";
}
if (positon.Trim().Length == 0)
{
positon = "-";
}
if (address.Trim().Length == 0)
{
address = "-";
}
if (email.Trim().Length == 0)
{
email = "-";
}
record cfRecord = new record();
string recordID = cfRecord.AddRecord(this.currentUserName, name, nickname, birth, sex, mobile, officetel, hometel, email, address, department, positon);
cfDGVRecord.Rows[currentRowIndex].Cells["RecordID"].Value = recordID;
currentRecordCount++;
contentFeedBack.Text = "记录共 " + currentRecordCount.ToString() + " 条";
}
catch
{ }
}
}
private void tsButtonSearch_Click(object sender, EventArgs e)
{
string keyword = tsTextBoxKeyword.Text;
if (keyword.Trim().Length > 0)
{
record searchRecord = new record();
DataSet ds = searchRecord.SearchRecordByKeywordUsername(keyword, currentUserName);
cfDGVRecord.DataSource = ds;
cfDGVRecord.DataMember = searchRecord.dbName;
cfDGVRecord.Columns["RecordID"].Visible = false;
cfDGVRecord.Columns["UserName"].Visible = false;
currentRecordCount = searchRecord.SearchRecordCountByKeywordUsername(keyword, currentUserName);
contentFeedBack.Text = "记录共 " + currentRecordCount.ToString() + " 条";
}
else
{
PopulateDataGridView();
}
}
private void tsButtonReturnAllRecord_Click(object sender, EventArgs e)
{
PopulateDataGridView();
}
- window form 练习使用datagridview
- 使用window.open传递form
- 在Window Form中使用NHibernate
- WPF Window 与 Winform Form混合使用方式
- Window Form Printer
- window form 截屏
- DataGridView使用
- DataGridView使用
- DataGridView使用
- DataGridView 使用
- form表单练习代码
- form表单小练习
- C#中的DataGridView控件(WIN FORM)
- c# datagridview 基本操作 (windows form)
- c# datagridView,form常用属性总结
- 多线程响应与 Window Form
- window form input validation && errorProvider
- Custom data binding, window form
- 零起点学VB做外挂(四)
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- 面向对象术语回顾
- picture_view
- huyuwei
- window form 练习使用datagridview
- 如何学习Hibernate
- 图片上传(加水印、缩略图、远程保存)的简单例子
- 用数据库按照树型结构存储系统参数
- CoM+日志错误 Help!!!
- sql基本函数
- Testlink 1.7.0 RC1 中文输入问题
- 使用Spring更好地处理Struts动作
- 工作流模式详解之流程控制模式(2)——Parallel Split