datagridview的数据源的操作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; namespace TechnologyProject.项目管理.加计扣除.ERP导入 { public partial class superDeduction : Form { DataGridViewRow row = new DataGridViewRow();//科研项目表的选红行 DataSet ds_Data = new DataSet();//excel DS DataTable datatable_ERP = null;//ERP导入DS Hashtable hs_superDeductionERP = new Hashtable();//datagridview public superDeduction() { InitializeComponent(); this.cmbMatchShow.SelectedIndex = 0; hs_superDeductionERP.Add("ID", "ID"); hs_superDeductionERP.Add("项目唯一标识", "kj9000a"); hs_superDeductionERP.Add("局级项目编号", "kj0065a"); hs_superDeductionERP.Add("厂处级项目编号", "kj0068a"); hs_superDeductionERP.Add("项目名称", "kj8101a"); hs_superDeductionERP.Add("项目内码", "kj8102a"); hs_superDeductionERP.Add("项目编号", "kj8103a"); hs_superDeductionERP.Add("二级单位", "kj8104a"); hs_superDeductionERP.Add("专业", "kj8105a"); hs_superDeductionERP.Add("项目级别", "kj8106a"); hs_superDeductionERP.Add("年度", "kj8107a"); hs_superDeductionERP.Add("标识", "kj8108a"); hs_superDeductionERP.Add("加计扣除标识", "kj8128a"); hs_superDeductionERP.Add("加计扣除类型", "kj8129a"); hs_superDeductionERP.Add("条款内容", "kj8130a"); hs_superDeductionERP.Add("8大项类型", "kj8403a"); hs_superDeductionERP.Add("金额", "kj8114a"); hs_superDeductionERP.Add("备注", "kj8115a"); btnCheck.Enabled = false; btnSave.Enabled = false; } public void FormShow(DataGridViewRow Srow) { row = Srow; this.ShowDialog(); } #region 选择Excel private void btnChoose_Click(object sender, EventArgs e) { try { OpenFileDialog openFD = new OpenFileDialog();//打开文件 openFD.Filter = "所有 Office Excel 文件|(*.xls;*.xl*;*.xlt;*.xlsx;*.xlc;*.xlw)";//选择器 if (openFD.ShowDialog() != DialogResult.OK) return; this.txtPath.Text = openFD.FileName; if (!System.IO.File.Exists(this.txtPath.Text))//如果要导入的名字和选中的不同,返回 return; PrintStatement print = new PrintStatement(); string filedir = string.Empty; string[] tablenames = print.Print_ERP导入(ref ds_Data, this.txtPath.Text);//数据源和路径 if (tablenames == null) return; SetAllSheet(tablenames);//通过数据库文件路径获取所有的Excel工作簿名称 btnCheck.Enabled = true; } catch (Exception ex) { } } /// <summary>获取所有的Excel工作簿名称</summary> void SetAllSheet(string[] tabnames) { datatable_ERP = SetDT(ds_Data);//处理excel DS BindDataSource(datatable_ERP); } //处理数据表 private DataTable SetDT(DataSet ds) { DataTable dt = new DataTable(); dt.Columns.Add("kj0083a");//项目名称 dt.Columns.Add("kj8403a");//8大项类型 dt.Columns.Add("kj8114a");//金额 dt.Columns.Add("kj0086a");//专业 dt.Columns.Add("kj0087a");//年度 dt.Columns.Add("kj0084a");//项目级别 dt.Columns.Add("kj9000a");//项目唯一标识 dt.Columns.Add("kj0065a");//局级项目编号 dt.Columns.Add("kj0068a");//厂处级项目编号 dt.Columns.Add("kj8128a");//加计扣除标识 dt.Columns.Add("kj8129a");//加计扣除类型 dt.Columns.Add("kj8130a");//条款内容 dt.Columns.Add("kj8112a");//八大项内码 dt.Columns.Add("kj8113a");//排序号 DataTable dtData = ds.Tables[0]; for (int i = 0, j = 0; i < dtData.Rows.Count; i++) { if (string.IsNullOrEmpty(dtData.Rows[i][0].ToString())) { continue; }//如果8大项类型为空,Excel导入的DataTable开始循环下一行,跳出当前i的所在循环 dt.Rows.Add(); dt.Rows[j]["kj0083a"] = row.Cells["xmmc"].Value.ToString();//项目名称 dt.Rows[j]["kj0086a"] = row.Cells["zy"].Value.ToString();//专业 dt.Rows[j]["kj0087a"] = row.Cells["nd"].Value.ToString();//年度 dt.Rows[j]["kj0084a"] = row.Cells["xmjb"].Value.ToString();//项目级别 dt.Rows[j]["kj8403a"] = dtData.Rows[i][0];//8大项类型 dt.Rows[j]["kj8114a"] = dtData.Rows[i][1];//金额 dt.Rows[j]["kj9000a"] = row.Cells["ID"].Value.ToString();//项目唯一标识 dt.Rows[j]["kj0065a"] = row.Cells["kj0065a"].Value.ToString();//局级项目编号 dt.Rows[j]["kj0068a"] = row.Cells["kj0068a"].Value.ToString();//厂处级项目编号 dt.Rows[j]["kj8128a"] = row.Cells["kj8128a"].Value.ToString();//加计扣除标识 dt.Rows[j]["kj8129a"] = row.Cells["kj8129a"].Value.ToString();//加计扣除类型 dt.Rows[j]["kj8130a"] = row.Cells["kj8130a"].Value.ToString();//条款内容 j++; } return dt; } //绑定数据源 private void BindDataSource(DataTable dtSource) { this.dataGV1.AutoGenerateColumns = false; dataGV1.DataSource = dtSource; } #endregion #region 显示匹配项改变事件 private void cmbMatchShow_SelectedIndexChanged(object sender, EventArgs e) { try { BindDataSouce(cmbMatchShow.Text); SetDGVColor(); } catch (Exception ex) { } } public void BindDataSouce(string combotext) { string RowFilter = string.Empty; DataView dv = datatable_ERP.DefaultView; if (combotext == "已匹配") RowFilter = "Flag is not null"; else if (combotext == "未匹配") RowFilter = "Flag is null"; dv.RowFilter = RowFilter; this.dataGV1.AutoGenerateColumns = false; this.dataGV1.DataSource = dv; } public void SetDGVColor() { //改变当前单元格的颜色 foreach (DataGridViewRow dgvr in this.dataGV1.Rows) { DataRowView drv = (DataRowView)dgvr.DataBoundItem; if (string.IsNullOrEmpty(drv["Flag"].ToString())) dgvr.DefaultCellStyle.ForeColor = System.Drawing.Color.Black; else dgvr.DefaultCellStyle.ForeColor = System.Drawing.Color.Blue; } } #endregion #region 导入数据 private void btnSave_Click(object sender, EventArgs e) { try { if (dataGV1.CurrentRow == null) return; Cursor.Current = Cursors.WaitCursor; DataSet ds = new DataSet();//实例化一个ds DataTable dt = datatable_ERP.Copy();//将datagridview的datatable复制,再赋给dt DataView dv = dt.DefaultView;//将dt的默认表的视图赋给dv dv.RowFilter = "Flag is null";//dataview的过滤器,Flag不等于空,排除Row的方法 dt.Columns.Remove("Flag");//排除列的方法 dt.Columns.Remove("kj8403a"); ds.Tables.Add(dv.ToTable());//将dv转换为表,加到ds里 OperaterClass.NewInsert(Const.LISTNAME_JJCC, Const.CONTENTTYPE_JJCC, ref ds, UserInfo.Department); this.btnSave.Enabled = false; this.btnCheck.Enabled = false; MessageBox.Show("已成功导入 " + ds.Tables[0].Rows.Count + " 条信息!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("ERP数据导入失败!\n\r请检查您选择的Excel模板是否正确!\n\r。系统提示:" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } #endregion #region 检查数据 private void btnCheck_Click(object sender, EventArgs e) { /*如何在一个datagridview中绑定数据源拼凑成另一张表 * 数据源本身来源于一个导入的Excel数据源和传入的DataRow数据组成的一张DataTable, * 利用Excel数据源内容从字典表中查到的id,id加到目前的DataTable * 将这个DataTable的数据存到数据库中 * for()循环要查的内容dr * { * for()固定的数据字典dr * {break;}内容在数据字典中找到,得到id,break跳出for循环 * * for()用得到的id到另一张表中查询,如果有表示查询到了数据,isExists = true,dr * {} * * 如果isExists = true,在内容表中标记这列为已将存在 * 将id加到内容dr中 * } */ try { btnSave.Enabled = true; //得到八大项字典表数据 DataTable dtBDX = OperaterClass.SearchData(Const.LISTNAME_JJKC8DXZDB, Const.CONTENTTYPE_JJKC8DX, "", null).Tables[0]; //根据项目名称,得到该项目对应的所有加计扣除列表数据 CamlBuilder cbl = new CamlBuilder(); cbl.AddWhere("Eq", "kj0083a", row.Cells["xmmc"].Value.ToString(), "Text"); //DataTable dtJJKC = OperaterClass.SearchData(Const.LISTNAME_JJCC, Const.CONTENTTYPE_JJCC, cbl.GetCamlString(), null).Tables[0]; DataSet dsJJKC = OperaterClass.SearchData(Const.LISTNAME_JJCC, Const.CONTENTTYPE_JJCC, cbl.GetCamlString(), null); DataTable dtJJKC = new DataTable(); if (dsJJKC != null) { dtJJKC = dsJJKC.Tables[0]; } datatable_ERP.Columns.Add("Flag");//标识列,表示改加计扣除项是否存在 for (int i = 0; i < datatable_ERP.Rows.Count; i++) { string id = string.Empty;//8大项ID string paNum = string.Empty;//排序号 string lx = datatable_ERP.Rows[i]["kj8403a"].ToString();//8大项类型 for (int j = 0; j < dtBDX.Rows.Count; j++) { if (lx == "1、仪器" && dtBDX.Rows[j]["kj8402a"].ToString() == "10") { lx = "1、仪器1"; } else if (lx == "2、设备" && dtBDX.Rows[j]["kj8402a"].ToString() == "10") { lx = "2、设备1"; } else if (lx == "1、仪器" && dtBDX.Rows[j]["kj8402a"].ToString() == "13") { lx = "1、仪器2"; } else if (lx == "2、设备" && dtBDX.Rows[j]["kj8402a"].ToString() == "13") { lx = "2、设备2"; } if (lx == dtBDX.Rows[j]["kj8403a"].ToString()) { id = dtBDX.Rows[j]["ID"].ToString(); paNum = dtBDX.Rows[j]["kj8405a"].ToString(); } if (!string.IsNullOrEmpty(id)) break;//如果找到了,就跳出for循环 } bool isExists = false;//加计扣除项是否存在 for (int k = 0; k < dtJJKC.Rows.Count; k++) { if (id == dtJJKC.Rows[k]["kj8112a"].ToString()) isExists = true; if (isExists) break; } if (isExists) { datatable_ERP.Rows[i]["Flag"] = "1"; } datatable_ERP.Rows[i]["kj8112a"] = id; datatable_ERP.Rows[i]["kj8113a"] = paNum; id = string.Empty; paNum = string.Empty; } BindDataSouce(cmbMatchShow.Text); SetDGVColor(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }
break,return,continue的使用区别
continue是跳出开始循环的一次新迭代,如下:
for(var i=1;i<=10;i++) {
if(i==6)
{
continue;
} document.write(i); } //输出结果:1234578910
break是跳出包含在最内层循环的循环体或者退出一个switch语句,如下:
for(var i=1;i<=10;i++) {
if(i==6)
{
break;
} document.write(i); } //输出结果:12345
在datagridview中查找到某条数据,更改这条数据
if (UpdataDataSet != null && UpdataDataSet.Tables.Count != 0 && UpdataDataSet.Tables[0].Rows.Count != 0)
{
//2013年11月7号
for (int i = 0; i < UpdataDataSet.Tables[0].Rows.Count; i++)
{
if (UpdataDataSet.Tables[0].Columns[i].ColumnName == "kj0064a")
{
UpdataDataSet.Tables[0].Columns[i].ColumnName = "kj0064a1";
break;
}
}
bool updata = OperaterClass.Updata(Const.CONTENTTYPE_JJCC, Const.CONTENTTYPE_JJCC, ref UpdataDataSet);
}
return是用于指定函数返回的值。return语句只能出现在函数体内,出现在代码中的其他任何地方都会造成语法错误!当执行return语句时,即使函数主体中还有其他语句,函数执行也会停止!
selecte,new Row,itemarray,SreachData的使用
public static DataSet ReCreateErJiDw(DataSet SourDS,string BmName) {
//得到“单位简称”等于BmName的二级单位的所有DataRow DataSet ERJIDWS = OperaterClass.SearchData("二级单位列表", "二级单位", "", null,string.Empty); DataRow[] drs = ERJIDWS.Tables[0].Select("kj0172a ='" + BmName + "'");//在DataTable中查出“单位简称”等于BmName的DataRow,排除Row的方法
if (drs.Length > 0)//如果有等于BmName的二级单位,这个二级单位的“单位性质”是上市,将传过来的sourDS的“项目级别”改为分公司,否则为勘探局 { foreach(DataRow dr in SourDS.Tables[0].Rows) { if (drs[0]["kj0177a"].ToString() == "上市") dr["kj0084a"] = "分公司"; else dr["kj0084a"] = "勘探局"; } } return SourDS;
DataSet ERJIDWS = OperaterClass.SearchData("二级单位列表", "二级单位", "", null, string.Empty); DataRow[] drs = ERJIDWS.Tables[0].Select("kj0172a ='" + "开发部" + "'"); DataTable tb = ERJIDWS.Tables[0]; DataRow dr= tb.NewRow();//得到一个和ERJIDWS的DataSet的表的列,一样的DataRow,相当于复制了一行空数据行 dr["kj0172a"] = drs[0]["kj0172a"];//给空数据行的某列赋值
DataSet SearchXMDS = null;//20130424新增 CamlBuilder cbl = new CamlBuilder(); cbl.AddWhere("Neq", "kj0084a", "厂处级", "Choice"); string query = cbl.GetCamlString(); SearchXMDS = OperaterClass.SearchData(Const.LISTNAME_KYXM, Const.CONTENTTYPE_KYXM, query,
new string[] { "ID","Title","kj0065a","kj0096a","kj0084a","kj0085a", "kj0090a", "kj0113a", "kj0114a", "kj8036a" });//得到指定“列”、“栏”,满足caml语句,指定“column”的DS MessageBox.Show(SearchXMDS.Tables[0].Rows.Count.ToString());
this.Cursor = Cursors.WaitCursor;//光标 if (!CheckIsRight()) return;//检查控件的值 bool isTrue = false;//是否成功 DataSet dsSave = Zeda.AssistantClass.DataSetOperator.CreateDS<DB_018车位费收费记录表>(); drcost = dsSave.Tables[0].NewRow(); SetSaveDSValue();//设置全局变量DataRow类型drcost的值,drcost为一个空行 dsSave.Tables[0].Rows.Add(drcost.ItemArray);//将drcost加到dsSave中,使用drcost直接加到一张表中,就不能再加到其他表中
//DataRow.ItemArray 属性
//通过一个数组来获取或设置此行的所有值。
//DataTable.NewRow 方法
//创建与该表具有相同架构的新 DataRow。
if (FormAssistant.IsDsNull(dsSave)) return; isTrue = DataService.Instance.SaveParkingFeesChargingRecordsInfo(ref dsSave); drcost = null; if (isTrue) { this.DialogResult = DialogResult.OK; this.Close(); }
private void SetSaveDSValue() { drcost["DB0003A"] = this.txtDB0004A.Tag;//小区ID drcost["DB0004A"] = this.txtDB0004A.EditValue;//小区名字 drcost["DB0119A"] = this.txtDB0049A.Tag;//车库ID drcost["DB0049A"] = this.txtDB0049A.EditValue;//车库 }
DataSet ERJIDWS = OperaterClass.SearchData("二级单位列表", "二级单位", "", null, string.Empty); DataRow[] drs = ERJIDWS.Tables[0].Select("kj0172a ='" + "开发部" + "'"); DataTable tb = ERJIDWS.Tables[0]; DataRow dr= tb.NewRow();//得到一个和ERJIDWS的DataSet的表的列,一样的DataRow,相当于复制了一行空数据行 dr["kj0172a"] = drs[0]["kj0172a"];//给空数据行的某列赋值 tb.Rows.Add(dr); DataTable tbe = ERJIDWS.Tables[0].Copy();//表的复制,包括内容 tbe.Rows.Add(dr);//不能将dr再加到tbe表中,它已经属于tb表,DataRow是引用类型
对datagridview的单元格的操作
foreach (DataGridViewRow dgvr in baseDgvItems.Rows)//设置Linklable的值,循环datagridview的每行 { foreach (DataGridViewCell dgvc in dgvr.Cells)//循环单元格的每个单元格 { if (dgvc is DataGridViewLinkCell) { DataRowView drv = (DataRowView)dgvr.DataBoundItem;//不同的datagridview的行的转换 DataGridViewColumn dgvColumn = baseDgvItems.Columns[dgvc.ColumnIndex];//根据单元格可以得到单元格的列明 string currentField = dgvColumn.Name; string currentText = dgvColumn.HeaderText; string dataPropertyName = dgvColumn.DataPropertyName; if (!string.IsNullOrEmpty(dataPropertyName)) { continue; } if (!string.IsNullOrEmpty(drv[currentField].ToString())) dgvc.Value = currentText; } } }
private void dgvItems_CellClick(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == -1 || e.RowIndex == -1) return; if (((DataGridView)sender).Columns[e.ColumnIndex] is DataGridViewLinkColumn) ;//判断单击的单元格是否为datagridviewlinkcolumn,触发事件的使用 { DataRowView drv = (DataRowView)((DataGridView)sender).Rows[e.RowIndex].DataBoundItem;//sender为dgvItem,e为cell DataGridViewColumn dgvc = ((DataGridView)sender).Columns[e.ColumnIndex]; if (!string.IsNullOrEmpty(dgvc.DataPropertyName)) { if (drv["kj9000a"] != null && !string.IsNullOrEmpty(drv["kj9000a"].ToString())) { ProgramDetail pd = new ProgramDetail(); pd.FormShow(drv["kj9000a"].ToString()); } } else { ShowPageForm Spf = new ShowPageForm(); string url = drv[dgvc.Name].ToString(); if (url == "") return; url = url.Substring(0, url.IndexOf(",")); Spf.Formshow(url); } } }
protected bool SaveAuditResult(string[] columnName, string[] value, string message) { if (this.baseDgvItems.Rows.Count <= 0) return false; if (columnName.Length != value.Length) { ControlOpt.ShowMessage("列数和值数不匹配,请确认!"); return false; } this.Cursor = Cursors.WaitCursor; try { DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ID", typeof(string)));//在datagridview中加一列 foreach (string cn in columnName) { dt.Columns.Add(new DataColumn(cn, typeof(string))); } foreach (DataGridViewRow dgvr in baseDgvItems.Rows) //更新审查的阶段状态,构建要更新的DataSet { if (Convert.ToBoolean(dgvr.Cells["cb"].Value) == false) continue; DataRow dr = ((DataRowView)dgvr.DataBoundItem).Row; DataRow newDR = dt.NewRow(); newDR["ID"] = dr["ID"].ToString(); for (int i = 0; i < columnName.Length; i++) { newDR[columnName[i]] = value[i]; } dt.Rows.Add(newDR); } ds.Tables.Add(dt); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { ControlOpt.ShowMessage("请勾选列表中要操作的项!"); return false; } if (MessageBox.Show("确定要执行" + message + "操作吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return false; bool updata = OperaterClass.Updata(listName, contentType, ref ds); if (!updata) { ControlOpt.ShowMessage(message + "保存失败!"); return false; } for (int i = baseDgvItems.Rows.Count - 1; i >= 0; i--)//在datagridview中删除选中行 { if (baseDgvItems.Rows[i].Cells["cb"].FormattedValue.ToString() == "True") { drv = (DataRowView)baseDgvItems.Rows[i].DataBoundItem; SearchDataSet.Tables[0].Rows.Remove(drv.Row); } } return true; } catch (Exception ex) { ControlOpt.ShowMessage(ex.Message); return false; } finally { this.Cursor = Cursors.Default; } }
删除datagridview的行
public void DeleteDataGridViewItems(DataGridView dgv, string TipMessages) { Cursor.Current = Cursors.WaitCursor; DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ID", typeof(string))); foreach (DataGridViewRow dgvr in dgv.Rows) { if (Convert.ToBoolean(dgvr.Cells["cb"].Value) == false) continue; DataRow dr = dt.NewRow(); DataRowView drv = (DataRowView)dgvr.DataBoundItem; dr["ID"] = drv["ID"].ToString(); dt.Rows.Add(dr);//取得所有选中的行的ID的DataTable }
if (dt.Rows.Count <= 0) { MessageBox.Show("请选中要删除的" + TipMessages + "!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (MessageBox.Show("确定要删除这些" + TipMessages + "吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; try { DataSet ds = new DataSet(); ds.Tables.Add(dt); if (OperaterClass.NewDelete(listName, contentType, ds, null))//将DataTable的加入到DataSet中,把DataSet给删除的接口,删除数据库中的数据 { for (int i = dgv.Rows.Count - 1; i >= 0; i--)//将选中的行在现在的datagridview中的数据源中删除行 { if (Convert.ToBoolean(dgv.Rows[i].Cells["cb"].Value) == false) continue; dgv.Rows.RemoveAt(i); } // MessageBox.Show("外协单位删除成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(TipMessages + "外协单位删除失败!失败的原因如下:" + ex.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } Cursor.Current = Cursors.Default; }
得到DataTable的行号,去除行号这行数据
List<int> drindex = new List<int>();//存放行号 for (int j = dt.Rows.Count - 1; j >= 0; j--) { DataRow dr = dt.Rows[j]; for (int i = 0; i < DsJJCC.Tables[0].Rows.Count; i++) { if (dr["Title"].ToString() == DsJJCC.Tables[0].Rows[i]["kj0083a"].ToString())//已存在 { if (dt.Rows.Count > 0) { drindex.Add(j);//将行号加到泛型数据中 } } } } for (int m = 0; m < drindex.Count; m++) { dt.Rows.RemoveAt(drindex[m]);//去除指定行 }
DataTable的排序问题
在C#中要对Datatable排序,可使用DefaultView的Sort方法。先获取Datatable的DefaultView,然后设置 得到的Dataview的sort属性,最后用视图的ToTable方法将排好序的dataview导出为Datatable。
DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Rows.Add(new object[] { 12, "lwolf" }); dt.Rows.Add(new object[] { 100,"kkkkk"}); dt.Rows.Add(new object[] { 19,"jim" }); dt.Rows.Add(new object[] { 1,"test" }); DataTable dtCopy = dt.Copy(); DataView dv = dt.DefaultView; dv.Sort = "ID"; dtCopy = dv.ToTable();
给DataTable赋值,foreach和index++
int index = 0; foreach (DataRow dr in SearchDataSet.Tables[0].Rows) { if (dr["kj0084a"].ToString() == "厂处级") { dr["kj0068a"] = Code[index].Remove(Code[index].Length-1); index++; } }
在datagridview中加载本地图片
/// <summary> /// 设置审阅状态图片 /// </summary> /// <param name="dgv"></param> public static void SetStatePicture(DataGridView dgv, string columnName) { foreach (DataGridViewRow dgvr in dgv.Rows) { DataRowView drv = (DataRowView)dgvr.DataBoundItem; //如果没有在:Datagrid练习.Properties.Resources下加载,让Resources.designer.cs生成这个图片的信息,否则是找不到的 dgvr.Cells[columnName].Value = (Image)global::Datagrid练习.Properties.Resources.ResourceManager.GetObject("已审阅"); //dgvr.Cells[dgvColumnName].Value = global::Datagrid练习.Properties.Resources.ResourceManager.GetObject("查看详细"); } }