首页 新闻 会员 周边 捐助

小弟不才,在写一个winform的项目出现了一个简单的问题,跪求各位大神帮忙

0
悬赏园豆:5 [已关闭问题] 关闭于 2017-09-06 11:52

 

随后附上代码 :由于最近在做开票的项目,很简单的问题就是根据从后台存储的字段到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;
}

}

 

这个问题就是由于在窗体首次出现中和按钮点击事件中都新增了"支付方式"这一列才会出现这个问题  可是现在我也想不出来其他好的办法做

无敌土豆的主页 无敌土豆 | 初学一级 | 园豆:80
提问于:2017-09-05 10:22
< >
分享
所有回答(3)
0

Insert 中的2代表第三列,列表中已经有第三列,所以报错已经存在,用update 进行修改第三列就行了。

悟行 | 园豆:12559 (专家六级) | 2017-09-05 10:29
0

new 要写在for 里面。你这错误是for 执行第二次就出错的。

爱编程的大叔 | 园豆:30844 (高人七级) | 2017-09-05 10:30

些在foreach里直接是 "未将对象引用到设置对象的实例"

支持(0) 反对(0) 无敌土豆 | 园豆:80 (初学一级) | 2017-09-05 10:38

@无敌小土豆呀: 你这个有问题吧,如果有多行的话,岂不是新增了很多列???因为不管多少行你只要新增一列就好了,所以把新增列放到循环外面,for里面只负责给单元格赋值

支持(0) 反对(0) jqw2009 | 园豆:2341 (老鸟四级) | 2017-09-05 11:26
0

  在属性columns集合里面把需要的列全部加进去,不要动态生成就好了

jqw2009 | 园豆:2341 (老鸟四级) | 2017-09-05 11:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册