在vs. net的开发里,不得不说DataGridView控件是很强大而好用,最近做项目,有了三点新的学习,现在记录如下:
一:在单元格里实现下拉选择框
原理很简单:就是定义一个DataGridViewComboBoxCell,并给其赋值(一般是一个数据源DataTabel),而后将它绑定指定的单元格就可以了。请看代码:
/// <summary>
/// 绑定ComBoBox列表到DataGridView控件里
/// </summary>
/// <param name="dgv"></param>
private void InitDataGridViewComBoBox(int rowNumber,DataGridView dgv)
{
DataRow drRows;
//数据通道下来列表的绑定
DataTable dtPrint_Data = new DataTable();
dtPrint_Data.Columns.Add("Print_Data");
drRows = dtPrint_Data.NewRow();
drRows.ItemArray = new string[] { "Print_data" };
dtPrint_Data.Rows.Add(drRows);
DataGridViewComboBoxCell dgvComBoBoxPrint_Data = new DataGridViewComboBoxCell();
dgvComBoBoxPrint_Data.DisplayMember = "Print_Data";
dgvComBoBoxPrint_Data.ValueMember = "Print_Data";
dgvComBoBoxPrint_Data.DataSource = dtPrint_Data;
//字体的绑定
DataTable dtFontNames = new DataTable();
dtFontNames.Columns.Add("FontNames");
foreach (FontFamily ff in FontFamily.Families)
{
drRows = dtFontNames.NewRow();
drRows.ItemArray = new string[] { ff.Name };
dtFontNames.Rows.Add(drRows);
}
DataGridViewComboBoxCell dgvComBoBoxFontNames = new DataGridViewComboBoxCell();
dgvComBoBoxFontNames.DisplayMember = "FontNames";
dgvComBoBoxFontNames.ValueMember = "FontNames";
dgvComBoBoxFontNames.DataSource = dtFontNames;
//绑定类型
DataTable dtType = new DataTable();
dtType.Columns.Add("Type");
drRows = dtType.NewRow();
drRows.ItemArray = new string[] { "文本" };
dtType.Rows.Add(drRows);
drRows = dtType.NewRow();
drRows.ItemArray = new string[] {"字段"};
dtType.Rows.Add(drRows);
drRows = dtType.NewRow();
drRows.ItemArray = new string[] { "国标码" };
dtType.Rows.Add(drRows);
drRows = dtType.NewRow();
drRows.ItemArray = new string[] {"内部码"};
dtType.Rows.Add(drRows);
drRows = dtType.NewRow();
drRows.ItemArray = new string[] {"图片" };
dtType.Rows.Add(drRows);
DataGridViewComboBoxCell dgvComBoBoxTypes = new DataGridViewComboBoxCell();
dgvComBoBoxTypes.DisplayMember = "Type";
dgvComBoBoxTypes.ValueMember = "Type";
dgvComBoBoxTypes.DataSource = dtType;
//字段名称绑定
DataTable dtDBText = new DataTable();
dtDBText.Columns.Add("DBTextX");
string strSelectPrint_FieldNames = "select chinesename from Print_FieldNames;";
DataSet dtSelectPrint_FieldNames = myCom.DataSelectReader(strSelectPrint_FieldNames, sqlLocalhost);
if (dtSelectPrint_FieldNames.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dtSelectPrint_FieldNames.Tables[0].Rows.Count; i++)
{
drRows = dtDBText.NewRow();
drRows.ItemArray = new string[] { dtSelectPrint_FieldNames.Tables[0].Rows[i][0].ToString() };
dtDBText.Rows.Add(drRows);
}
}
DataGridViewComboBoxCell dgvComBoBoxDBTextX = new DataGridViewComboBoxCell();
dgvComBoBoxDBTextX.DisplayMember = "DBTextX";
dgvComBoBoxDBTextX.ValueMember = "DBTextX";
dgvComBoBoxDBTextX.DataSource = dtDBText;
//将它绑定到表格控件里去
dgv.Rows[rowNumber].Cells[3] = dgvComBoBoxPrint_Data;
dgv.Rows[rowNumber].Cells[7] = dgvComBoBoxFontNames;
dgv.Rows[rowNumber].Cells[1] = dgvComBoBoxTypes;
dgv.Rows[rowNumber].Cells[4] = dgvComBoBoxDBTextX;
}
二、注意结束编辑状态
请先看下图,我要选择面料成分(想选择2行数据),可是结果我取出的结果只有1行数据,因为第二行数据还处在编辑状态,.net认为你还没有结束呢,当然不会给你计算在内,遇到这样的情况,我们就需要先将编辑状态结束掉才可以正确的选择所要的数据,而所用的代码很简单,就一句:this.dataGridView1.EndEdit();//先结束编辑状态
三、删除DataGridView控件里的行
我们知道删除DataGrid里的指定行是比较简单的,使用this.dataGridView1.Rows.Remove(row);就可以了,如果要我们删除被选中的行(也许用户只选择了其行的一个单元格,他说我现在想把这行给删除了,或者他选择的行并不是连续的),那么我们该怎么办呢?
经过思考,可以这么办,请先看下下面的图
不多说原理了,直接代码上:
if (this.dataGridView1.Rows.Count > 1)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("您确定要删除所选的行么?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
//先检查一下模板的明细是否存在
string strSelectPrint_ModeMaster_Check = "select MasterID from Print_ModeMaster where name='" + this.txtModeName.Text.Trim() + "';";
DataSet dtSelectPrint_ModeMaster_Check = myCom.DataSelectReader(strSelectPrint_ModeMaster_Check, sqlLocalhost);
if (dtSelectPrint_ModeMaster_Check.Tables[0].Rows.Count > 0)
{
//再检查一下明细的记录是否存在
for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
{
this._DeleteRowsArrayList.Add(dtSelectPrint_ModeMaster_Check.Tables[0].Rows[0][0].ToString() + "&" + this.dataGridView1.SelectedRows[i].Cells[0].Value.ToString());
}
}
//删除行的操作
foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
{
if (!row.IsNewRow)//判断是否是新行
{
this.dataGridView1.Rows.Remove(row);
}
}
}
}
else
{
if (MessageBox.Show("您确定要删除所选的行么?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
//选的单元格的行
ArrayList _SelectRowsArrayList = new ArrayList();
for (int i = 0; i < this.dataGridView1.SelectedCells.Count; i++)
{
_SelectRowsArrayList.Add(this.dataGridView1.SelectedCells[i].RowIndex);
}
ArrayList SelectRowsArrayList = myCom.ArrayListRepeatMove(_SelectRowsArrayList);
//MessageBox.Show(SelectRowsArrayList.Count.ToString());
//先检查一下模板的明细是否存在
string strSelectPrint_ModeMaster_Check = "select MasterID from Print_ModeMaster where name='" + this.txtModeName.Text.Trim() + "';";
DataSet dtSelectPrint_ModeMaster_Check = myCom.DataSelectReader(strSelectPrint_ModeMaster_Check, sqlLocalhost);
if (dtSelectPrint_ModeMaster_Check.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < this.dataGridView1.SelectedCells.Count; i++)
{
this._DeleteRowsArrayList.Add(dtSelectPrint_ModeMaster_Check.Tables[0].Rows[0][0].ToString() + "&" + this.dataGridView1[0, this.dataGridView1.SelectedCells[i].RowIndex].Value.ToString());
}
}
//删除行的操作
//this.ListBoxPrint_ModeMaster.SelectedItem = this.txtModeName.Text.Trim();
//this.ListBoxPrint_ModeMaster_SelectedValueChanged(sender,e);
ArrayList _myDeleteRows = myCom.ArrayListRepeatMove(this._DeleteRowsArrayList);
for (int j = 0; j < _myDeleteRows.Count; j++)
{
string strKey_Id = _myDeleteRows[j].ToString().Split(new char[] { '&' })[1];
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (!row.IsNewRow)
{
if (row.Cells[0].Value.ToString().Trim() == strKey_Id)
{
this.dataGridView1.Rows.Remove(row);
}
}
}
}
}
}
}
总结:DataGridView博大精深,值得我们好好去研究它的用法,如果有朋友有好的用法,还希望不吝告知!小子感激不尽!