Fill Listview From Database c#

1)Create Class File and Give Name "Funcation"

2)Past Code in class file "Funcation"


        public static void listview(ListView lst, string sql, SqlConnection sqlConnection)
        {
            SqlCommand cmd = new SqlCommand(sql, datastore.Connection());

            SqlDataReader dr = cmd.ExecuteReader();

            ListViewItem itmX;

            while (dr.Read())
            {
                itmX = new ListViewItem();
                itmX.Text = dr.GetValue(0).ToString();

                for (int i = 1; i < dr.FieldCount; i++)
                {
                    itmX.SubItems.Add(dr.GetValue(i).ToString());
                }
                lst.Items.Add(itmX);
            }
            dr.Close();
            datastore.CloseConnection();
        }


3)  past following code in project button event

   private void button2_Click(object sender, EventArgs e)
        {
            lstworklist.Items.Clear();
            function.listview(lstworklist, "select * from Specification", datastore.Connection());
        }

0 comments:

Post a Comment