本文介绍了如何在没有sql数据源的情况下将文本框数据绑定到数据网格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在Windows窗体上创建了以下控件。
TextBox - txtName
按钮 - btnAdd
DataGridView - gvTotalNames
当我点击btnAdd按钮时,txtName文本应为插入到网格视图中。
再次在txtName中输入一些数据应该在网格视图中显示另外一条记录。
如何实现这个目标?
我可以看到结果如
姓名
aaaa
bbbb
cccc
dddd
Hi All,
I created on windows form with following controls.
TextBox - txtName
Button - btnAdd
DataGridView - gvTotalNames
When I click "btnAdd" button the txtName text should be inserted in to grid view.
Again entered some data into the txtName one more record should be displayed in to grid view.
How can I achieve this?
I can see the result like
Name
aaaa
bbbb
cccc
dddd
推荐答案
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
gvTotalNames.Rows.Add()
gvTotalNames.Item(0, gvTotalNames.Rows.Count - 1).Value = txtName.Text
txtName.Text = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gvTotalNames.AllowUserToAddRows = False
gvTotalNames.Rows.Clear()
End Sub
private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable("TotalNames");
DataColumn dc = new DataColumn("Names");
dt.Columns.Add(dc);
gvTotalNames.DataSource = dt;
}
private void btnAdd_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["Names"] = txtName.Text;
dt.Rows.Add(dr);
}
这篇关于如何在没有sql数据源的情况下将文本框数据绑定到数据网格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!