如何在Gridview WInform中存储数据? [英] How to store data's in Array from Gridview WInform ?

查看:106
本文介绍了如何在Gridview WInform中存储数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,用户从下拉框中选择客户。我将客户发票填入gridview。现在我想将发票号和发票金额存储到数组中。如何完成这项任务?



我尝试了这段代码但只获得了最后一行gridview

Hi, In my form user select the customer from dropdown box. I populate the customer invoice's into gridview. Now I want to store that invoice no and invoice amount into array. How to complete this task ?

I tried this code but getting last row of gridview only

for (int i = 0; i < gridView2.RowCount; i++)
            {
                invno = gridView2.GetRowCellValue(i, "InvoiceId");

                int[] invid = new int[] { Convert.ToInt32(invno) };
            }

推荐答案

在这里,您可以找到有关.NET和C#中数组的一些信息:

http://msdn.microsoft.com/en-us/library/ aa288453%28v = vs.71%29.aspx [ ^ ]



我认为在这种情况下,数组不是一个好选择。为什么不使用Dictionary?

假设您在gridView中存储数据,如下所示:InvoiceId(Int32)和TotalAmount(Double),请尝试使用此代码:

Here you find some informations about arrays in .NET and C#:
http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx[^]

I think that array is not good choice in this case. Why not to use Dictionary?
Assuming that you are storing data in gridView in fields like this: InvoiceId (Int32) and TotalAmount (Double), try to use this code:
var invoices = new Dictionary<int,double>()
for (int i = 0; i < gridView2.RowCount; i++)
{
    invNo = gridView2.GetRowCellValue(i, "InvoiceId");
    invAmount = gridView2.GetRowCellValue(i, "TotalAmount");
    invoices.Add(invNumber, invAmount);
}





您可以像这样迭代字典:



The you can iterate through you dictionary like this:

foreach (int invoiceId in invoices.Keys)
{
    double invoiceAmount = invoices[invoiceId]
    Console.WriteLine(string.Format("Invoice: {0}, Total amount: {1}", invoiceId, invoiceAmount));
}





希望能帮助你:)



Hope that help you :)


这篇关于如何在Gridview WInform中存储数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆