none
listview databinding

    Question

  • i am working in windows 2003 application using c#.

    i want to use

    listView1.DataBindings property in c#.

    Anybody is having sample code.

    Please help me.

    Tuesday, June 19, 2007 6:25 AM

Answers

  •  

    Hi santosh,

     

    It is just like the TextBox.DataBindings. See my sample below.

    Code Snippet

            DataTable dt = new DataTable();

            private void Form1_Load(object sender, EventArgs e)

            {          

                dt.Columns.Add("height", typeof(int));

                dt.Rows.Add(12);

                dt.Rows.Add(120);

                dt.Rows.Add(220);

                dt.Rows.Add(320);

                dt.Rows.Add(500);

                dt.Rows.Add(600);

     

                this.listView1.DataBindings.Add("Height", dt, "height");

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                if (this.listView1.BindingContext[dt].Position != 0)

                    this.listView1.BindingContext[dt].Position--;

            }

     

            private void button2_Click(object sender, EventArgs e)

            {

                if (this.listView1.BindingContext[dt].Position != this.listView1.BindingContext[dt].Count - 1)

                    this.listView1.BindingContext[dt].Position++;

            }

     

    Hope this helps.

    Regards

    Thursday, June 21, 2007 2:35 AM

All replies

  •  

    Hi santosh,

     

    It is just like the TextBox.DataBindings. See my sample below.

    Code Snippet

            DataTable dt = new DataTable();

            private void Form1_Load(object sender, EventArgs e)

            {          

                dt.Columns.Add("height", typeof(int));

                dt.Rows.Add(12);

                dt.Rows.Add(120);

                dt.Rows.Add(220);

                dt.Rows.Add(320);

                dt.Rows.Add(500);

                dt.Rows.Add(600);

     

                this.listView1.DataBindings.Add("Height", dt, "height");

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                if (this.listView1.BindingContext[dt].Position != 0)

                    this.listView1.BindingContext[dt].Position--;

            }

     

            private void button2_Click(object sender, EventArgs e)

            {

                if (this.listView1.BindingContext[dt].Position != this.listView1.BindingContext[dt].Count - 1)

                    this.listView1.BindingContext[dt].Position++;

            }

     

    Hope this helps.

    Regards

    Thursday, June 21, 2007 2:35 AM
  • I Get the following exception, when i tried to do the same,

    The background of my program is like this

    i have a ListView control by identifier listviewData.
    ListView -> Details View.
    listViewData.Columns.Add("VendorId");

    I have a datatable 

    DataTable dt = new Datatable
    dt.columns.add("VendorId", typeof(int));


    When i try to add databindings to listView i get the following error. Sad



    System.ArgumentException was unhandled
      Message="Cannot bind to the property 'VendorId' on the target control.\r\nParameter name: PropertyName"
      Source="System.Windows.Forms"
      ParamName="PropertyName"
      StackTrace:
           at System.Windows.Forms.Binding.CheckBinding()
           at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
           at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
           at System.Windows.Forms.BindingsCollection.Add(Binding binding)
           at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider formatInfo)
           at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember)
           at DataBind_Example.Form1..ctor() in e:\home's Documents\Visual Studio 2005\Projects\DataBind Example\DataBind Example\Form1.cs:line 27
           at DataBind_Example.Program.Main() in e:\home's Documents\Visual Studio 2005\Projects\DataBind Example\DataBind Example\Program.cs:line 17
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    Sunday, October 19, 2008 6:28 PM
  •             DataSet d ;

                DataTable dt = new DataTable();
                dt = d.Tables[0];
                listView1.Columns.Add("Chasing", 100, HorizontalAlignment.Left);


                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem i = new ListViewItem(dr["chasing"].ToString());

                    i.Tag = dr;

                    listView1.Items.Add(i);
                }

    Wednesday, May 16, 2012 9:00 AM