First of all, it should be noted that there are already many articles on ListView data binding on the network, but most of them are for Winform, therefore, many new users may port code from Winform. net cf platform will feel very difficult, because many attributes or events are not supported, and recently the company has implemented a project, a lot of ListView needs to bind data, in addition, each ListView has a different column (field) binding. Alas, the following is my practice. Let's share it with you.
Let's look at a piece of code. First, we get the data source to a DataTable, and then, according to the number of columns in the Listview and the column name (usually, the column name already tells you what fields to bind to this column ~), We set the field binding:
Code
Private void DataBind ()
{
DataTable dt = bllMission. GetData (""); // obtain the data source
String [] str = new string [6]; // 6 indicates the number of columns in the ListView on the page. Known
MissLV. Items. Clear ();
Foreach (DataRow row in dt. Rows) // traverses each row of data and adds the corresponding field value to the corresponding column
{
Str [0] = row ["miss_id"]. ToString ();
Str [1] = row ["miss_name"]. ToString ();
Str [2] = row ["miss_state"]. ToString ();
Str [3] = row ["miss_contact"]. ToString ();
Str [4] = row ["miss_contactTel"]. ToString ();
Str [5] = row ["miss_address"]. ToString ();
ListViewItem item = new ListViewItem (str );
MissLV. Items. Add (item );
}
}
OK. After reading this code, you must have understood it. Maybe you will think that data binding is too rigid, right! In advance, we need to set the Column of Listview on the interface, then set the size of the str String Array Based on the number of columns, and finally bind the corresponding fields in each row to the corresponding columns.
However, there is no way. In our project, the UI is a group of people dedicated to it, and then another group of people dedicated to binding data and other business logic operations. I hope that experts can give their own opinions and share their experiences. What methods or methods have they used for Listview binding ~
After that, I think it can be summarized into a public class, so that I don't need to write this binding code over and over again. My public class is as follows:
Code
/// <Summary>
/// Listview data binding (specify field binding)
/// By Jack Fan
/// </Summary>
/// <Param name = "dt"> data source DataTable </param>
/// <Param name = "lvCounts"> Number of columns of the Listview to be bound </param>
/// <Param name = "field"> name of the field to be bound, separated by commas (,). The number of fields must correspond to the number of ListView columns </param>
/// <Param name = "lv"> ID of the ListView control to which data is bound </param>
Public static void ListViewBind (DataTable dt, int lvCounts, string field, ListView lv)
{
String [] str = new string [lvCounts];
Lv. Items. Clear ();
String [] fields = Utils. splitString (field, ","); // The SplitString class is used to split strings such as "abc, def, higk" and put the split results into a string array.
Try
{
Foreach (DataRow dr in dt. Rows)
{
For (int I = 0; I <= lvCounts; I ++)
{
Str [I] = dr [fields [I]. ToString ();
}
ListViewItem item = new ListViewItem (str );
Lv. Items. Add (item );
}
}
Catch (Exception e)
{
Throw new Exception (e. Message );
}
}
The above is just a small experience or practice in the project. This practice selects specific fields from the data source for data binding based on the existing ListView columns, which is not the best method, I have also read a brain approach (based on the number of fields in the data source, you can turn it into a Listview column, and then traverse it cyclically to add row data, which is simpler ~). I hope you can share your data binding experience in. net cf development. Thank you. I also hope that this article will help you.