I want to use e select query and show it in the listview.
Can someone help me in this part.
Thanks
I want to use e select query and show it in the listview.
Can someone help me in this part.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satish KathiRank #8,369 · 223 reputationPosted Jul 11, 2008, 10:24 AM EDT
{
using (SqlCommand myCommand = conn.CreateCommand()){
myCommand.CommandType = System.Data.
CommandType.Text;myCommand.CommandText =
"SELECT * FROM employee";conn.Open();
SqlDataReader dr = myCommand.ExecuteReader(); while (dr.Read()){
listView1.Items.Add(dr.GetString(1));
}
}
}
Arnold Petrona156 reputationPosted Jul 12, 2008, 11:24 AM EDT
Ok it works
But I'm trying to have all of my fields ( 3) from the database to the Listview1. each of them in their columnheader.
I saw some code but it is not a way to understand what they r doing
Satish KathiRank #8,369 · 223 reputationPosted Jul 14, 2008, 7:18 AM EDT
Here is sample code to create columns and add data from SQL to list view. Hope this helps and please tune the code
You can create columns to ListView within the code as shown below or you can add columns in Designer View
private void LstVw_Load(object sender, EventArgs e)
{
AddColumnsToListView();
string connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand myCommand = conn.CreateCommand())
{
myCommand.CommandType = System.Data.CommandType.Text;
myCommand.CommandText = "SELECT * FROM employee";
conn.Open();
SqlDataReader dr = myCommand.ExecuteReader();
while (dr.Read())
{
ListViewItem lvi = new ListViewItem( new string[] {dr[0] != System.DBNull.Value ? dr.GetInt32(0).ToString() : string.Empty,
dr[1] != System.DBNull.Value ? dr.GetString(1) : string.Empty,
dr[3] != System.DBNull.Value ? dr.GetString(3) : string.Empty});
//ListViewItem lvi = new ListViewItem(dr[0] != System.DBNull.Value ? dr.GetInt32(0).ToString() : string.Empty);
//lvi.SubItems.Add(dr[1] != System.DBNull.Value ? dr.GetString(1) : string.Empty );
//lvi.SubItems.Add(dr[3] != System.DBNull.Value ? dr.GetString(3) : string.Empty);
listView2.Items.Add(lvi);
}
}
}
}
private void AddColumnsToListView()
{
listView2.Columns.Add("Id", 30, HorizontalAlignment.Right);
listView2.Columns.Add("Name", 80, HorizontalAlignment.Left);
listView2.Columns.Add("Job", 50, HorizontalAlignment.Left);
}
Arnold Petrona156 reputationPosted Jul 21, 2008, 7:37 AM EDT
Can this way with the listview also possible with datagridview?
Satish KathiRank #8,369 · 223 reputationPosted Jul 21, 2008, 9:08 AM EDT
http://www.codeproject.com/KB/books/PresentDataDataGridView.aspx
Arnold Petrona156 reputationPosted Jul 21, 2008, 11:13 AM EDT
But i don't see the part without using Datasource. I want to do it with Select * statment and transfer it to the datagridview
Satish KathiRank #8,369 · 223 reputationPosted Jul 21, 2008, 11:57 AM EDT
The best way is to use the DataSource.
Here is the code as per your requirement(add columns to the data gridview either in design mode or before calling this code)
string connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ToString(); using (SqlConnection conn = new SqlConnection(connectionString)){
using (SqlCommand myCommand = conn.CreateCommand()){
myCommand.CommandType = System.Data.
CommandType.Text;myCommand.CommandText =
"SELECT * FROM employee";conn.Open();
SqlDataReader dr = myCommand.ExecuteReader(); while (dr.Read()){
dataGridView1.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
}
}
}
Arnold Petrona156 reputationPosted Jul 21, 2008, 2:29 PM EDT
Life Saver