Home > Net >  WPF to add data in the listview
WPF to add data in the listview

Time:09-15

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. The Data;
Using System. Windows. The Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Imaging;
Using System. Windows. Navigation;
Using System. Windows. Shapes;
using System.Data;

The namespace wpfDemo
{
///& lt; summary>
///MainWindow. Xaml interaction logic
///& lt;/summary>
Public partial class MainWindow: Window
{
The DataTable dt.
Public MainWindow ()
{
InitializeComponent();
Dt=data ();

ListView_T. DataContext=dt;
ListView_T. SetBinding (ListView. ItemsSourceProperty, new Binding ());
}
Public DataTable data ()
{
The DataTable r=new DataTable (" newtable ");

DataColumn c=new DataColumn [] [] {new DataColumn (" ID "), the new DataColumn (" Name "), the new DataColumn (" Age "), the new DataColumn (" Sex ")};
R.C olumns. AddRange (c);
return r;

}


Public void lvAddCol (string [] STR)
{
DataRow a=dt. NewRow ();
For (int j=0; J & lt; Dt. Columns. The Count; J++)
A [j]=STR [j];
Dt. Rows. The Add (a);
}

Private void button_Click (object sender, RoutedEventArgs e)
{
STR=new string string [] [] {" 0 ", "DFD", "dferrs", "wedf"};
LvAddCol (STR);
}
}
}

CodePudding user response:

It is suggested to carry out the binding to use MVVM, design good viewmodel; You will not directly set ItemsSource interaction logic class, the whole things,

CodePudding user response:

Will, of course, such use is not wrong, only need pay attention to the xaml interface binding field path

 & lt; GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/& gt; 

CodePudding user response:

To change the code name for well: data is clearly a noun, you have a data () method name? The DataTable object named why r? Why DataColumn array named c?
Said there is on the second floor of the 1st floor, since you use WPF why also use winform that set of coupling approach? According to if you now to give you a solution, is also a dirty special treatment!
  •  Tags:  
  • C#
  • Related