C # Data Binding

Source: Internet
Author: User
 One, ComboBox (from:http://hi.baidu.com/anymis/blog/item/89c1dd3d25c149c79f3d62b3.html) 
ComboBox bound data in C # is ComboBox bound data its data source is usually a DataTable this binding is relatively easy, this.combobox1.datasource=gettable ();//Data source

this.combobox1.displaymember= "Name"; this.combobox1.valuemember= "id"; And the one that's been trapping me is combobox1.add (object), where he can add a pair of objects that, if added to it, have no way of picking up his ID and name as the previous DataTable found that it was possible to do so today.

The data source is to get the data from the XML.

   ArrayList source=new ArrayList ();

   XmlNode Nodetablelist=xmldoc.selectsinglenode ("Tables");

     try {foreach (XmlNode xmlnod in nodetablelist) {mapsearchcondition msc=new mapsearchcondition (); MSc. Gistablename=xmlnod. selectSingleNode ("Gistablename").

     InnerText; MSc. Tabledescription=xmlnod. selectSingleNode ("Tabledescrip").

     InnerText; MSc. Tablename=xmlnod. selectSingleNode ("tablename").

     InnerText; MSc. Filedescription=xmlnod. selectSingleNode ("Filename/filedescript").

     InnerText; MSc. Filename=xmlnod. selectSingleNode ("Filename/filename").

      InnerText;

    Source.add (MSC);

   The catch (Exception e) {} This.cmbtype.datasource=source; This. cmbtype.displaymember= "Tabledescription"; 
 
ComboBox Experience Summary [statically add a piece of data after dynamic binding data] (from:)



    Dropdowstyle=dropdown allows the user to type a text box for a list item, and dropdownsysle=dropdownlist displays a list of items for the default value, from which the user can select a: Add an item to the property Iitems                 Cobshow.selectedindex the index of the selected item Cobshow.selecteditem the text of the selected item Cobshow.selecttext

    Null value Cobshow.selectvalue Error ... Cobshow.items[cobshow.selectedindex]. ToString () Select the text of the item B: Add CobShow.Item.Add () to the program the result is the same as the above c:a B two ways add the results as above D: Dynamic binding Data Co Bshow.selectedindex the index of the selected item Cobshow.selecteditem System.DataRow.DefaultView COBSHOW.SELECTT  Ext NULL Cobshow.selectvalue The value value of the selected item cobshow.text the text of the selected item Value Cobshow.items[cobshow.selectedindex]. ToString () System.DataRow.DefaultView E: If a value is written first through a property or program, the original value is emptied when dynamically bound: if the data is dynamically bound, after the program COBSHOW.ITEM.ADD (' Zhi '); Error...



   [Solution as follows] Fsubject is the text to display, Ftech is the value of the DataTable to be passed DT = Cboshow.datasource as DataTable; DataRow dr = dt.



          NewRow (); 

          dr["Fsubject"] = "---Text---";



          dr["Ftech"]= "---Value---"; Dt. Rows.insertat (DR, 0); Insert to the first line dt. Rows.Add (DR); Append to last line explanation: dr["Fsubject"],dr["Ftech" is a row of 2 columns, after which the entire row is added cobshow.text= "---Text---, cobshow.selectvalue="--- Value---"Tianshan Cold snow review: Fdatatable dt = ds." 

Tables[0]; DataRow dr = dt. 

NewRow (); 

dr["A"] = "0"; 

dr["B"] = "Please choose"; Dt. 





Rows.insertat (DR, 0); Cboshow. DataSource = ds. Tables[0]; This time will trigger an event cboshow. DisplayMember = "B"; This time will also trigger event cboshow. 





ValueMember = "A"; 



The above is a very good method, but when there are more than one row in the database, also want to assign a value? 



The following continues to discuss, if Cboshow have event Selectindexchange assign value when will trigger an event? Why to discuss the time, because we sometimes need to load the time does not trigger events, but when the choice of trigger events,//from above can see Datasource,displaymember will trigger an event, will not trigger//at this time selected Value has always been a System.Data.DataRowView;; Determine the value of Selectvalue directly in the event ... if (cboshow. 
Selectedvalue.tostring ()!= "System.Data.DataRowView")
{String cc = "111"; }//[2]//if (cboshow. 

Text!= "System.Data.DataRowView")//{//MessageBox.Show ("123"); 

 //}

Second, DataView

SqlConnection conn = theconn; Conn.             Open ();             DataSet ds = new DataSet ();             String cmdtext = "SELECT * from thetable";             SqlDataAdapter da = new SqlDataAdapter (Cmdtext,conn); Da.             Fill (ds, "mydaily"); Datagridview1.datasource = ds.             tables["Thetable"]; Conn. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.