ListView Add Data
-
I have my listview where I plot some data. I want to show the data to a table, so I am using listview. I do have problem using the subitem function. I can show my x data to the fist column, however I am having problem to show the y data to the next column here is my code
private void button1_Click(object sender, System.EventArgs e) { double[] dData = new double[200]; double[] dData2 = new double[200]; for(int i=0;i<200;i++) { dData[i] = Math.Sin(0.09*i); dData2[i] = Math.Cos(0.1*i); listView1.Items.Add(i.ToString()); //show the x data to the fist colunm however I couldn't find a way to show the sine and the cosine to the next colunm } waveformPlot1.PlotY(dData); waveformPlot2.PlotY(dData2); }
so my output looks like that col 1 col 2 col 3 0 blank blakk 1 blank blank show how can I show subitem in the listview -
I have my listview where I plot some data. I want to show the data to a table, so I am using listview. I do have problem using the subitem function. I can show my x data to the fist column, however I am having problem to show the y data to the next column here is my code
private void button1_Click(object sender, System.EventArgs e) { double[] dData = new double[200]; double[] dData2 = new double[200]; for(int i=0;i<200;i++) { dData[i] = Math.Sin(0.09*i); dData2[i] = Math.Cos(0.1*i); listView1.Items.Add(i.ToString()); //show the x data to the fist colunm however I couldn't find a way to show the sine and the cosine to the next colunm } waveformPlot1.PlotY(dData); waveformPlot2.PlotY(dData2); }
so my output looks like that col 1 col 2 col 3 0 blank blakk 1 blank blank show how can I show subitem in the listviewYou have to use ListViewItem Class. First you need to add the columns to ListViewItem and add the ListViewItem to the ListView Control.
Try this...
private void button1_Click(object sender, System.EventArgs e)
{
double[] dData = new double[200];
double[] dData2 = new double[200];
ListViewItem newItem = null;
for(int i=0;i<200;i++)
{
dData[i] = Math.Sin(0.09*i);
dData2[i] = Math.Cos(0.1*i);
newItem= new ListViewItem();
newItem.SubItems.Add(i.ToString());
newItem.SubItems.Add(dData[i].ToString());
newItem.SubItems.Add(dData2[i].ToString());
this.listView1.Items.Add(newItem);
} Cheers, jagan -
You have to use ListViewItem Class. First you need to add the columns to ListViewItem and add the ListViewItem to the ListView Control.
Try this...
private void button1_Click(object sender, System.EventArgs e)
{
double[] dData = new double[200];
double[] dData2 = new double[200];
ListViewItem newItem = null;
for(int i=0;i<200;i++)
{
dData[i] = Math.Sin(0.09*i);
dData2[i] = Math.Cos(0.1*i);
newItem= new ListViewItem();
newItem.SubItems.Add(i.ToString());
newItem.SubItems.Add(dData[i].ToString());
newItem.SubItems.Add(dData2[i].ToString());
this.listView1.Items.Add(newItem);
} Cheers, jagan -
I have tried it over and got it right; this was the only line that I missed this.listView1.Items.Add(newItem);
-
Hmmmm thats interesting... Anyway this is not right way but try this.. private void button1_Click(object sender, System.EventArgs e) { double[] dData = new double[200]; double[] dData2 = new double[200]; ListViewItem newItem = null; for(int i=0;i<200;i++) { dData[i] = Math.Sin(0.09*i); dData2[i] = Math.Cos(0.1*i); newItem= new ListViewItem(i.ToString()); newItem.SubItems[1].Text = dData[i].ToString(); newItem.SubItems[2].Text = dData2[i].ToString(); newItem.SubItems.Add(dData[i].ToString()); newItem.SubItems.Add(dData2[i].ToString()); this.listView1.Items.Add(newItem); } Cheers, Jagan
-
Hmmmm thats interesting... Anyway this is not right way but try this.. private void button1_Click(object sender, System.EventArgs e) { double[] dData = new double[200]; double[] dData2 = new double[200]; ListViewItem newItem = null; for(int i=0;i<200;i++) { dData[i] = Math.Sin(0.09*i); dData2[i] = Math.Cos(0.1*i); newItem= new ListViewItem(i.ToString()); newItem.SubItems[1].Text = dData[i].ToString(); newItem.SubItems[2].Text = dData2[i].ToString(); newItem.SubItems.Add(dData[i].ToString()); newItem.SubItems.Add(dData2[i].ToString()); this.listView1.Items.Add(newItem); } Cheers, Jagan
I received the following error on that line newItem.SubItems[1].Text = dData[i].ToString(); An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll Additional information: Specified argument was out of the range of valid values.
-
I received the following error on that line newItem.SubItems[1].Text = dData[i].ToString(); An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll Additional information: Specified argument was out of the range of valid values.
However by removing the two lines, the rest of the code works fine. This is the one that works
private void button1_Click(object sender, System.EventArgs e) { double[] dData = new double[200]; double[] dData2 = new double[200]; ListViewItem newItem = null; for(int i=0;i<200;i++) { dData[i] = Math.Sin(0.09*i); dData2[i] = Math.Cos(0.1*i); newItem= new ListViewItem(i.ToString()); newItem.SubItems.Add(dData[i].ToString()); newItem.SubItems.Add(dData2[i].ToString()); this.listView1.Items.Add(newItem); } waveformPlot1.PlotY(dData); waveformPlot2.PlotY(dData2); }
-
However by removing the two lines, the rest of the code works fine. This is the one that works
private void button1_Click(object sender, System.EventArgs e) { double[] dData = new double[200]; double[] dData2 = new double[200]; ListViewItem newItem = null; for(int i=0;i<200;i++) { dData[i] = Math.Sin(0.09*i); dData2[i] = Math.Cos(0.1*i); newItem= new ListViewItem(i.ToString()); newItem.SubItems.Add(dData[i].ToString()); newItem.SubItems.Add(dData2[i].ToString()); this.listView1.Items.Add(newItem); } waveformPlot1.PlotY(dData); waveformPlot2.PlotY(dData2); }