Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# datatable to listview

I like to be able to view datatable in windows form

I managed to get the headers only with ListView how to get the data in there

DataTable data = new DataTable();

data = EnumServices();

//create headers
foreach (DataColumn column in data.Columns)
{
      listView_Services.Columns.Add(column.ColumnName);
}

I just want to show now the data in there!

cheers

like image 335
Data-Base Avatar asked May 04 '10 12:05

Data-Base


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

foreach (DataRow row in data.Rows)
{
    ListViewItem item = new ListViewItem(row[0].ToString());
    for (int i = 1; i < data.Columns.Count; i++)
    {
        item.SubItems.Add(row[i].ToString());
    }
    listView_Services.Items.Add(item);
}

Update: also, if you're calling your method more than once, you need to either clear the columns collection before adding the columns, or check to see if the columns have already been added - otherwise, the number of columns will keep increasing every time you call your method.

like image 173
MusiGenesis Avatar answered Oct 17 '22 11:10

MusiGenesis


I've been looking for solution to this but I failed. I wasn't able to find any that would best answer to this problem, so I made my own solution. I am so much glad that I was able to make this solution as shorter, easier and more understandable yet would best answer to what most of us looking for.

    public void ShowCurrentParked()
    {
        dt3 = new DataTable();

        dt3.Clear();    //clear to avoid overlapping data (new DataTable is not enough)

        lstViewShowCurrentParked.Items.Clear();  //clear items to accept new or updated data to avoid overlapping/duplicate data

        dt3 = pBAL.ShowCurrentParked();  //datasource (class)
        for (int j = 0; j < dt3.Rows.Count; j++)
        {
            lstViewShowCurrentParked.BeginUpdate();
            lstViewShowCurrentParked.Items.Add(new ListViewItem(new string[] {dt3.Rows[j][1].ToString(),dt3.Rows[j][2].ToString(),
        dt3.Rows[j][3].ToString()}));
        }

        lstViewShowCurrentParked.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        lstViewShowCurrentParked.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

        lstViewShowCurrentParked.EndUpdate();

        dt3.Clear();   //clear again to avoid overlapping data (new DataTable is not enough) .. to make datatable really empty


        //BeginUpdate and EndUpdate will just lessen the flicker during listview update
    }

Efficiency: 1. fast 2. avoid data duplication 3. less flicker

like image 26
user5146033 Avatar answered Oct 17 '22 09:10

user5146033