如何在网格视图中填充数据到文本框 [英] How Can I Populate Data In Grid View To Textbox
本文介绍了如何在网格视图中填充数据到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyDBApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Server=127.0.0.1;Database=SampleDB;User id=sa;password=sa2008;");
private void Form1_Load(object sender, EventArgs e)
{
try
{
con.Open();
FillData();
}
catch (SystemException ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox2.Text + ",'" + textBox3.Text + "','" + textBox1.Text + "')", con))
{
cmd.ExecuteNonQuery();
}
FillData();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
void FillData()
{
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Emp_Details", con))
{
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
}
}
}
}
使用此代码我可以在文本框中输入数据,它将自动填充在网格视图中。
现在我想要当我点击网格视图中的特定行时,数据必须填充在文本框中。
任何人都可以帮我解决这个问题。
谢谢。
Using this code i can able to enter the data in textboxes, it will automatically populated in grid view.
Now i want when i click on particular row in grid view the data must populate in text boxes.
Can any one help me to solve this issue.
Thanks.
推荐答案
试试这个: -
try this:-
textBox1.Text = dataGridView1.CurrentRow.Cells["Column name"].Value.ToString();
或
or
textBox1.Text = dataGridView1.CurrentRow.Cells[Column index].Value.ToString();
使用dataGridView1的CellClick事件并尝试上面的代码:
use CellClick event of dataGridView1 and try the above code:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value ;
}
试试这样
Try like this
textBox.Text = dataGridView.SelectedRows(0).Cells(cellIndex).Value.ToString()
参考:
如何将gridview选中的行值添加到另一个窗口形式的文本框 [ ^ ]
希望这可能会有所帮助。
Refer:
how to get gridview selected row value to text box which is in another windows form[^]
Hope this may help.
这篇关于如何在网格视图中填充数据到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文