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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Arnold PetronaPosted Jul 21, 2008, 6:29 PM
Life Saver
Satish KathiPosted Jul 21, 2008, 3:57 PM
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 PetronaPosted Jul 21, 2008, 3:13 PM
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 KathiPosted Jul 21, 2008, 1:08 PM
http://www.codeproject.com/KB/books/PresentDataDataGridView.aspx
Arnold PetronaPosted Jul 21, 2008, 11:37 AM
Can this way with the listview also possible with datagridview?
Satish KathiPosted Jul 14, 2008, 11:18 AM
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 PetronaPosted Jul 12, 2008, 3:24 PM
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 KathiPosted Jul 11, 2008, 2:24 PM
{
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));
}
}
}