Click here to Skip to main content
14,668,708 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi
i am using the C#.net in wpf(windows application)
in that iam using a list view with two columns name as item1 and item2 ,
i want to add the fields from the database basing on condition
i am facing the problem is how to add the it to the listview
i am not finding the and option like rows and columns please help me

<br />
 i had  created  dynamically but  every thing is running good but the at the  listview  display only one item is  showing


OleDbConnection mcon = new OleDbConnection("provider =Microsoft.JET.OLEDB.4.0;data source=D:\\invoice\\invoice.mdb");
          OleDbCommand da = new OleDbCommand("select * from invoicetable where invoiceno='" + invoicenoxearchtxt.Text + "'order by ID asc", mcon);
          OleDbDataReader dr;
          mcon.Open();
          dr = da.ExecuteReader();
          ListItem la = new ListItem();
          DataSet ds = new DataSet();
          DataTable dt = ds.Tables.Add("dt");
          dt.Columns.Add("companyname", typeof(System.String));
          dt.Columns.Add("customername", typeof(System.String));

          // Create the GridView


          if (dr.Read())
          {
              DataRow dr1 = dt.NewRow();
              dr1["companyname"] = dr["companyname"].ToString();
              dr1["customername"] = dr["customername"].ToString();
              dt.Rows.Add(dr1);

              //_searchcollection.Add(new searchdata
              //{
              //    companyname = dr["companyname"].ToString(),
              //    customername = dr["customername"].ToString()
              //});
          }
           // Create the GridView Columns
           GridView gv = new GridView();
          // Setup the GridView Columns
          foreach (DataColumn item in ds.Tables[0].Columns)
          {
              GridViewColumn gvc = new GridViewColumn();
              gvc.DisplayMemberBinding = new Binding(item.ColumnName);
              gvc.Header = item.ColumnName;
              gvc.Width = 150f;
              gv.Columns.Add(gvc);
          }

          listView1.View = gv;
          // Display the Data
          listView1.DataContext = ds.Tables["dt"].DefaultView;
         // listView1.Items.Add(_searchcollection);
         // listView1.DataContext = ds.Tables[0].DefaultView;
         // ListViewEmployeeDetails.DataContext = ds.Tables[0].DefaultView;
         // listView1.Items.Add(_searchcollection);

         mcon.Close();



i had also created the columns in xaml and i had bindeb to the companyname and customername

coding in xamal: note here i had removes the "<>"
ListView Canvas.Left="433" Canvas.Top="147" Height="250" <br />
Name="listView1" Widt h="317.826" SelectionMode="Single"  ItemsSource="{Binding}" <br />
 listview<br />
            <br />

please sugesst me how to do this thing

thank q in advance
Posted
Updated 22-Sep-10 11:46am
v3
Comments
Abhishek Sur 22-Sep-10 16:04pm
   
I think you need to create the listview dynamically.
sivakumarmr10 22-Sep-10 17:08pm
   
how can i do that on e please say the process
and the given link is not opening

Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to use Loop to create columns for the ListView.

You can check
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5e2159f6-032b-4601-9e1e-ee403af3ad7[^]

Here the Columns are created dynamically, so that when the datatable is bound you need to recreate columns totally.

:rose:
   
Comments
sivakumarmr10 23-Sep-10 1:20am
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Rate this:
Please Sign up or sign in to vote.

Solution 2

DataSet ds = new DataSet();
               DataTable dt = ds.Tables.Add("dt");
               dt.Columns.Add("companyname", typeof(System.String));
               dt.Columns.Add("customername", typeof(System.String));
               while (dr.Read())
               {
                   DataRow dr1 = dt.NewRow();
                   dr1["companyname"] = dr["companyname"].ToString();
                   dr1["customername"] = dr["customername"].ToString();
                   dt.Rows.Add(dr1);
               }
               GridView gv = new GridView();
               foreach (DataColumn item in ds.Tables[0].Columns)
               {
                   GridViewColumn gvc = new GridViewColumn();
                   gvc.DisplayMemberBinding = new Binding(item.ColumnName);
                   gvc.Header = item.ColumnName;
                   gvc.Width = 150f;
                   gv.Columns.Add(gvc);
               }
               listView1.View = gv;
               listView1.DataContext = ds.Tables["dt"].DefaultView;



THANK Q MY PROBLEM IS SOLVED THANKQ FOR THE ATTENTION IT WAS CREATED DYNAMICALLY WITH OUT USING XAML
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100