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();
        }

原创粉丝点击