随后附上代码 :由于最近在做开票的项目,很简单的问题就是根据从后台存储的字段到girdview 表格根据字段转换成图片(小弟刚进公司实习)
//窗体首次显示时发生
private void PaidForm_Shown(object sender, EventArgs e) {
var orderinfoAll = Service.ServerHelper.GetOrderListAll();
grid_viewOrder.DataSource = orderinfoAll;
if (orderinfoAll.Count > 0)
{
DataGridViewImageColumn dvi = new DataGridViewImageColumn();
dvi.Name = "payType1";
dvi.HeaderText = "支付方式";
dvi.Width = 100;
foreach (var item in orderinfoAll)
{
if (item.PayType == Business.Rules.PayType.Cash)
{
dvi.Image = Properties.Resources.现金;
grid_viewOrder.Columns.Insert(2, dvi);
grid_viewOrder.Rows[0].Cells["payType1"].Value = dvi.Image;
}
if (item.PayType == Business.Rules.PayType.Aipay)
{
dvi.Image = Properties.Resources.支付宝;
grid_viewOrder.Columns.Insert(2, dvi);
grid_viewOrder.Rows[0].Cells["payType1"].Value = dvi.Image;
}
if (item.PayType == Business.Rules.PayType.WXpay)
{
dvi.Image = Properties.Resources.微信;
grid_viewOrder.Columns.Insert(2, dvi);
grid_viewOrder.Rows[0].Cells["payType1"].Value = dvi.Image;
}
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
panelLoadingData.Visible = true;
try
{
panelNoData.Visible = false;
panelSource.Visible = true;
string buyerName = this.txtBuyerName.Text;
string invoiceNo = this.txtInvoiceNo.Text;
DateTime? beginTime = null;
DateTime? endTime = null;
if (!string.IsNullOrEmpty(dateInvoiceBeginDate.Text))
{
beginTime = Convert.ToDateTime(dateInvoiceBeginDate.Text);
}
if (!string.IsNullOrEmpty(dateInvoiceEndDate.Text))
{
endTime = Convert.ToDateTime(dateInvoiceEndDate.Text);
}
var orderList = Service.ServerHelper.GetOrderList(Business.Rules.OrderStatus.InvoicePrinted, buyerName, invoiceNo, beginTime, endTime).OrderByDescending(x => x.InvoiceDate).ToList();
if (orderList.Count > 0)
{
orderInfoBindingSource.DataSource = orderList;
panelSource.Visible = true;
DataGridViewImageColumn dvic = new DataGridViewImageColumn();
dvic.Name = "payType";
dvic.HeaderText = "支付方式";
dvic.Width = 100;
foreach (var item in orderList)
{
if (item.PayType == Business.Rules.PayType.Cash)
{
dvic.Image = Properties.Resources.现金;
grid_viewOrder.Columns.Insert(2, dvic);
grid_viewOrder.Rows[0].Cells["payType"].Value = dvic.Image;
grid_viewOrder.Columns.RemoveAt(3);
}
if (item.PayType == Business.Rules.PayType.Aipay)
{
dvic.Image = Properties.Resources.支付宝;
grid_viewOrder.Columns.Insert(2, dvic);
grid_viewOrder.Rows[0].Cells["payType"].Value = dvic.Image;
grid_viewOrder.Columns.RemoveAt(3);
}
if (item.PayType == Business.Rules.PayType.WXpay)
{
dvic.Image = Properties.Resources.微信;
grid_viewOrder.Columns.Insert(2, dvic);
grid_viewOrder.Rows[0].Cells["payType"].Value = dvic.Image;
grid_viewOrder.Columns.RemoveAt(3);
}
}
}
else
{
panelNoData.Visible = true;
panelSource.Visible = false;
}
}
catch (Exception ex)
{
Yodoo.Common.LogHelper.Write(ex.Message);
}
finally
{
panelLoadingData.Visible = false;
}
}
这个问题就是由于在窗体首次出现中和按钮点击事件中都新增了"支付方式"这一列才会出现这个问题 可是现在我也想不出来其他好的办法做
Insert 中的2代表第三列,列表中已经有第三列,所以报错已经存在,用update 进行修改第三列就行了。
new 要写在for 里面。你这错误是for 执行第二次就出错的。
些在foreach里直接是 "未将对象引用到设置对象的实例"
@无敌小土豆呀: 你这个有问题吧,如果有多行的话,岂不是新增了很多列???因为不管多少行你只要新增一列就好了,所以把新增列放到循环外面,for里面只负责给单元格赋值
在属性columns集合里面把需要的列全部加进去,不要动态生成就好了