简体   繁体   English

如何将CSV文件值对齐到列表视图列

[英]How to align csv file values into listview columns

how do align the values from the csv file into my listview control... the problem in the code I used is that every csv values are added in the first column only... 如何将csv文件中的值对齐到我的listview控件中...我使用的代码中的问题是,每个csv值仅添加在第一列中...

this is my sample listview output: 这是我的示例listview输出: 在此处输入图片说明

the first "1234" needs to be aligned with "asd" but on the second column "p" 第一个“ 1234”需要与“ asd”对齐,但在第二列“ p”上

then the second "1234" needs to be aligned with "dfg" but on the second column "p" 那么第二个“ 1234”需要与“ dfg”对齐,但在第二列“ p”上

The correct display needs to be like this: 正确的显示必须是这样的:

u | p p

   asd   |    1234

   dfg   |   1234

The "|" “ |” symbol represents only a border in the listview control between the 2 values... sorry for the misunderstanding.. 符号仅表示两个值之间的列表视图控件中的边框...造成误解。

Here is the code that I have constructed so far... 这是我到目前为止构建的代码...

try
{

    string delimiter = ",";
    string tablename = "medTable";
    DataSet dataset = new DataSet();
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
    openFileDialog1.FilterIndex = 1;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {


        if (MessageBox.Show("Are you sure you want to import the data from \n " + openFileDialog1.FileName + "?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            filename = openFileDialog1.FileName;
            StreamReader sr = new StreamReader(filename);
            string csv = File.ReadAllText(openFileDialog1.FileName);


            sr.Close();


            String[] strArray = csv.Split(new char[] { ',' });
            foreach (string strValue in strArray)
            {
                listView1.Items.Add(strValue);
            }

        }
        else
        {

        }
    }

}

catch (Exception ek)
{

}

I know there are a lot of gurus here who are very helpful ... thanks 我知道这里有很多大师很乐于助人...谢谢

Try to replace the foreach statement with this block 尝试用此块替换foreach语句

int i = 0;
string val = string.Empty;
foreach (string strValue in strArray)
{
    val += strValue + "  ";
    i++;
    if (i == 2)
    {
        listView1.Items.Add(val);
        i = 0;
        val = string.Empty;
    }

}

i hope this help you 希望对您有帮助

EDIT To add the items in 2 columns as displayed in picture you already posted please use this and i hope it works fine 编辑要将项目添加到您已发布的图片中显示的2列中,请使用它,我希望它能正常工作

 int i = 0;
 ListViewItem item = new ListViewItem();
 foreach (string strValue in strArray)
 {
    i++;
    if (i == 1)
    {
       item = new ListViewItem();
       item.Text = strValue;
    }
    if (i == 2)
    {
        item.SubItems.Add(strValue);
        listView1.Items.Add(item);
        i = 0;
    }
 }

You want to create a SubItem for the second value, right? 您要为第二个值创建一个SubItem吗? Make sure the ListView has two Columns and is in Details view. 确保ListView有两Columns ,并且在Details视图中。

listView1.View = View.Details;
listView1.Columns.Add("header1");
listView1.Columns.Add("header2");

Although I prefer the designer to do that.. Now you can fill it: 尽管我希望设计师这样做。.现在,您可以填充它:

using System.IO;
//..
var lines = File.ReadAllLines(filename);
foreach (string line in lines)
{
    var parts = line.Split(',');
    ListViewItem lvi = new ListViewItem(parts[0]);
    lvi.SubItems.Add(parts[1]);
    listView1.Items.Add(lvi);
}

You may want to add error checking before accessing the parts array! 您可能要在访问部件阵列之前添加错误检查!

A note on using the SubItems collection: When you later access it the SubItem[1] is the 2nd column and SubItem[0] is the first column, which you have actually added as the Item ! 关于使用SubItems集合的注释:以后访问它时, SubItem[1]是第二列, SubItem[0]是第一列,您实际上已将其添加为Item

If you simply wanted the two values be concatenated, you would have changed my code to this: 如果您只是想将两个值串联在一起,则可以将我的代码更改为:

var lines = File.ReadAllLines(filename);
foreach (string line in lines)
{
    var parts = line.Split(',');
    listView1.Items.Add(parts[0] + "  -  " + parts[1]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM