Click here to Skip to main content
15,891,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what I have, and all column are automatically created and name of db field is name in header, now I want manually to insert just two column and bind those two column to column from data set.

How I do that?




// Execute and return the rows in the data reader object
OleDbDataReader dataReader;
dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);

int nFields = dataReader.FieldCount;

// Setup the columns in the listview using the fields in the table
lstDatas.Clear();
for (int i = 0; i<nFields; i++)
{
					lstDatas.Columns.Add(dataReader.GetName(i), 100, HorizontalAlignment.Left);
}

// Fill the rows in the listview using the data in the rows
int nRow = 0;
while (dataReader.Read())
{
// Create an array of subitems for quick insertion
// The subitems will be all fields in the row except for
// the first field
String [] subitems = new String[nFields];
for (int i = 0; i < nFields; i++)
{
subitems[i] = dataReader[i].ToString();
}

// Insert a new item into the listview, and add the subitems at
// the same time. The item will be the first field in the row
ListViewItem item = new ListViewItem(subitems, -1);
	
lstDatas.Items.Add(item);

++nRow;
}
dataReader.Close();

				

	
}
finally
{
// Close the connection if necessary
if (Connection.State == System.Data.ConnectionState.Open)
Connection.Close();
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900