Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How do I display data automatically from a selected datagridview row to a text box without declaring values ​​for separate text boxes, just need the column name of the table and the name of the same text box?

$
0
0

This is my idea but it doesn't work

DataTable dt = new DataTable ();

          

dt.Columns.Add ("_ ID"); 
            dt.Columns.Add ("_ Tên"); 
            dt.Columns.Add ("_ Danh tên"); 

            dt.Rows.Add ("0", "duy", "hungyen"); 
            dt.Rows.Add ("1", "duy1", "hungyen1"); 
            dt.Rows.Add ("2", "duy2", "hungyen2"); 
            dt.Rows.Add ("3", "duy3", "hungyen3"); 
            dt.Rows.Add ("4", "duy4", "hungyen4"); 
            dt.Rows.Add ("5", "duy5", "hungyen5"); 
            dt.Rows.Add ("6", "duy6", "hungyen6"); 

            dữ liệuGridView1.



 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int n = this.Controls.Count;
            int n1 = dt.Rows.Count; 
            int n2 = dt.Columns.Count; 
            for (int i = 0; i <= n - 1; i ++) 
            { 
                for (int j = 0; j <= n1 - 1; j ++) 
                { 
                    for (int k = 0; k <= n2 - 1; k ++) 
                        if (this.Controls [i] .Name == dt.Columns [k] .ColumnName) 
                        { 
                            this.Controls [i] .Text = dataGridView1.Rows [j] .Cells [k] .Value .ToString (); 
                        } 
                    } 
                } 
                dataGridView1.DataSource = dt;


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>