2

I am currently developing a C# Windows Form Application.

Now I am trying to use a SQL Command to retrieve information from the database to fill in the information that I need to have in my Application.

A sample query would be "select * from Location"

In the Location table there would be variables like locationId, LocationName , districId etc etc. I used the following code

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("connectionstring");
    SqlDataAdapter ada = new SqlDataAdapter("select * from MasterLocation", con);
    DataTable dt = new DataTable();
    ada.Fill(dt);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataRow dr = dt.Rows[i];
        ListViewItem listitem =new ListViewItem(dr["pk_Location_ID"].ToString());
        listitem.SubItems.Add(dr["var_Location_Name"].ToString());
        listitem.SubItems.Add(dr["fk_int_District_ID"].ToString());
        listitem.SubItems.Add(dr["fk_int_Company_ID"].ToString());
       listView1.Items.Add(listitem);
    } 

The output is:

like this.

but it should be like this:

sql output

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Cindrella
  • 343
  • 3
  • 5
  • 16

3 Answers3

5

you have to change some code

private void button1_Click(object sender, EventArgs e)
{
    listView1.View = View.Details;
    SqlConnection con = new SqlConnection("connectionstring");
    SqlDataAdapter ada = new SqlDataAdapter("select * from MasterLocation", con);
    DataTable dt = new DataTable();
    ada.Fill(dt);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataRow dr = dt.Rows[i];
        ListViewItem listitem = new ListViewItem(dr["pk_Location_ID"].ToString());
        listitem.SubItems.Add(dr["var_Location_Name"].ToString());
        listitem.SubItems.Add(dr["fk_int_District_ID"].ToString());
        listitem.SubItems.Add(dr["fk_int_Company_ID"].ToString());
       listView1.Items.Add(listitem);
    } 
Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
2

enter image description here

Added the following code

listView1.View = View.Details;

and it worked.

Cindrella
  • 343
  • 3
  • 5
  • 16
0
 private void FormView_Load(object sender, EventArgs e)
  {
    sample = new DataTable(); //Sample Data
            sample.Columns.Add("id", typeof(string));
            sample.Columns.Add("name", typeof(string));
            sample.Rows.Add("1", "apple");
            sample.Rows.Add("2", "acer");
            sample.Rows.Add("3", "alpha");
            sample.Rows.Add("4", "beat");
            sample.Rows.Add("5", "ball");
            sample.Rows.Add("6", "cat");
            sample.Rows.Add("7", "catch");
            sample.Rows.Add("10", "zebra");

            listViewEx1.View = View.Details;
            listViewEx1.Columns.Add("id");
            listViewEx1.Columns.Add("name");
  }



         listViewEx1.Items.Clear();

            listViewEx1.FullRowSelect = true;

            foreach (DataRow row in sample.Rows)
            {
                    ListViewItem item = new ListViewItem(row["id"].ToString());
                    item.SubItems.Add(row["name"].ToString());
                    listViewEx1.Items.Add(item); //Add this row to the ListView
             }
RameezAli
  • 956
  • 11
  • 12