C#winform Displays the contents of the first column cell of the selected row, DataGridView control

Source: Internet
Author: User

1 UI

2 Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten  One namespaceWindowsFormsApplication1 A { -      Public Partial classForm1:form -     { the          PublicForm1 () -         { - InitializeComponent (); -         } +  -         Private voidForm1_Load (Objectsender, EventArgs e) +         { A  atlist<test> datas =NewList<test>(); -Datas. ADD (NewTest () {Name ="Dry", age ="1" }); -  -             //binding Data -Datagridview1.datasource =datas; -             in         } -         classTest to         { +             Private string_name; -             Private string_age; the  *              Public stringName $             {Panax Notoginseng                 Get -                 { the                     return_name; +                 } A  the                 Set +                 { -_name =value; $                 } $             } -  -              Public string Age the             { -                 GetWuyi                 { the                     return_age; -                 } Wu  -                 Set About                 { $_age =value; -                 } -             } -         } A  +         Private voidButton1_Click (Objectsender, EventArgs e) the         { -             //Datagirdview Control selects the last row of multiple rows the first cell value string expression $             stringContent = datagridview1.selectedrows[0]. cells[0]. Value.tostring (); the MessageBox.Show (content); the  the             //in some cases, the order of the columns does not correspond to the 0 1 2 index values.  the             //I wrote in another program, the index value of the first column is 1, and the second column is 0. I don't understand. -         } in     } the}

3 Show

The problem that the column does not correspond to the index value appears in the order of the property members of the class. Can make a test.

C#winform Displays the contents of the first column cell of the selected row, DataGridView control

Related Article

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.