一、DataGridView数据转成DataTable
1.已绑定过数据源:
DataTable dt = (dataGridView1.DataSource as DataTable)
2.未绑定过数据源:
public DataTable GetDgvToTable(DataGridView dgv)
{
DataTable dt = new DataTable();
// 列强制转换
for (int count = 0; count < dgv.Columns.Count; count++)
{
DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
dt.Columns.Add(dc);
}
// 循环行
for (int count = 0; count < dgv.Rows.Count; count++)
{
DataRow dr = dt.NewRow();
for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
{
dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
}
dt.Rows.Add(dr);
}
return dt;
}
二、
1.刷新:重新绑定
2.清空:绑定null
3、 dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; 第一列隐藏
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 行选中模式
4、动态绑定列名
foreach (DataColumn dataColumn in AppDataSet.Tables[0].Columns)
{
DataColumnSrv dataColumnSrv = new DataColumnSrv(dataColumn);
if (dataColumnSrv.IsQuery)
{
myStyleDataGridView1.Columns.Add(dataColumn.ColumnName, dataColumn.Caption);
myStyleDataGridView1.Columns[dataColumn.ColumnName].DataPropertyName = dataColumn.ColumnName;
myStyleDataGridView1.Columns[dataColumn.ColumnName].ReadOnly = true;
}
}
三、
1.声明全局变量对象:public object Message { get; set; }
2.选中当前行
DataRow dataRow = (this.myDataGridView1.CurrentRow.DataBoundItem as DataRowView).Row;
3、存储在泛型List<T>对象中
List<DataRow> list = new List<DataRow>() { dataRow };
this.Message = list;
4.变量保存选中的行
List<string> rets = new List<string>();
foreach (DataGridViewRow item in dgv.Rows)
{
if (item.Cells[0].Value + "" == "1")
{
rets.Add(item.Cells["CZDepart"].Value + "");
}
}
this.Message = rets;
5、List中去重复,并用"-"连接起来
List<string> rets = pop.Message as List<string>;
if (rets != null)
{
DataRow dataRow = FormUtil.GetCurrentRow(AppBindingSources, dgv.BindTable);
if (dataRow == null)
return;
string col003 = dataRow["Col_003"] + "";
if (!col003.IsNullOrWhiteSpace())
{
rets.AddRange(col003.Split(‘-‘));
}
rets.Remove("");
dataRow["Col_003"] = string.Join("-", rets.Distinct());//去重复,并用"-"连接起来
}
6.选定行转DataTable
DataTable dtTemp = dgv1.SelectedDataRows.CopyToDataTable();
7.DataGrideView转到DatTable
public static DataTable DataTableChangeColumnName(MyDataGridView dgv, DataTable dtImport)
{
if (dtImport == null) { return null; }
DataTable dtCheck = null;
//列名集合找到,替换
dtImport.TableName = dgv.BindTableName;
dtCheck = dtImport.Copy();
foreach (DataColumn dc in dtCheck.Columns)
{
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dc.ColumnName.Trim().Equals(dgv.Columns[i].HeaderText.Trim()))
{
dc.ColumnName = dgv.Columns[i].DataPropertyName.Trim();
}
}
}
return dtCheck;
}
8.编辑状态下,取消事件
//编辑状态下
private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (dgv.Columns[e.ColumnIndex].Name == "Col_003")
{
e.Cancel = true;
}
}
9、 键值对用法
汇总
private void getTotal()
{
dict.Clear();
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
if ((dataGridView1.Rows[i].Cells["IsSelect"].Value + "").ToUpper() == "TRUE")
{
if (!dict.ContainsKey(dataGridView1.Rows[i].Cells["公司名称"].Value + "") || !dict.ContainsValue(dataGridView1.Rows[i].Cells["类型"].Value + ""))
dict.Add(dataGridView1.Rows[i].Cells["公司名称"].Value + "", dataGridView1.Rows[i].Cells["类型"].Value + "");
}
}
}
遍历
foreach (KeyValuePair<string, string> kvp in dict)
{
if (kvp.Value.Equals("取"))
{
mSql += string.Format("UPDATE Table1 SET PickDiffDegree ={0} WHERE Company=N‘{1}‘ ;" + Environment.NewLine, mNdNumber, kvp.Key);
}
}