如何从文本框中读取数据并显示到Gridview中,而不会覆盖Gridview中的现有行数据 [英] How to read the data from textboxes and display into the Gridview without overwritting the existing row data in Gridview
本文介绍了如何从文本框中读取数据并显示到Gridview中,而不会覆盖Gridview中的现有行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void btnGrid_Click(对象发件人,EventArgs e)
{
// 显示文本框数据到Gridview
dt = new DataTable();
dt.Columns.Add( name);
dt.Columns.Add( age is);
string na = txtName.Text;
int age = Convert.ToInt32(txtAge.Text);
dt.Rows.Add(na,age);
GridView1.DataSource = dt;
GridView1.DataBind();
// DataRow dr1 = dt.NewRow();
// dr1 [0] = txtName.Text;
// dr1 [1] = txtAge.Text;
// dt.Rows.Add(dr1);
// GridView1.DataSource = dt;
// GridView1.DataBind();
}
解决方案
计算Gridview行,然后使用计算的行值添加值
,如下面的代码(C#Windows应用程序代码)
string dates = textBox1.Text.ToString();
int i = dataGridView1.Rows.Count;
int j = i-1;
this.dataGridView1.Rows.Add();
dataGridView1.Rows [j] .Cells [0] .Value = dates;
textBox1.Text = ;
谢谢
公共部分类测试:System.Web.UI.Page
{
DataTable tb = new DataTable();
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
createtable();
}
protected void Button1_Click(object发件人,EventArgs e)
{
tb =(DataTable)ViewState [table1];
tb.Rows.Add(TextBox1.Text,tb.Rows.Count + 1);
GridView1.DataSource = tb;
GridView1.DataBind();
}
public void createtable()
{
tb.Columns.Add(Name,typeof(string));
tb.Col umns.Add(Id,typeof(int));
ViewState [table1] = tb;
}
}
public partial class test2:System.Web.UI.Page
{
静态DataTable table1 = new DataTable();
protected void Page_Load(object sender,EventArgs e)
{
< br $>
}
protected void Button1_Click(object sender,EventArgs e)
{
Int32 Rcount = table1.Rows。数;
if(Rcount == 0)
{
table1.Columns.Add(FName );
table1.Rows.Add(TextBox1.Text);
}
else
{
DataRow dr = table1.NewRow();
dr [FName] = TextBox1.Text;
table1.Rows.Add(dr);
}
GridView1.DataSource = table1;
GridView1.DataBind();
}
}
protected void btnGrid_Click(object sender, EventArgs e)
{
//Displaying Textboxes data into Gridview
dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("age is");
string na = txtName.Text;
int age = Convert.ToInt32(txtAge.Text);
dt.Rows.Add(na, age);
GridView1.DataSource = dt;
GridView1.DataBind();
//DataRow dr1 = dt.NewRow();
//dr1[0] = txtName.Text;
//dr1[1] = txtAge.Text;
//dt.Rows.Add(dr1);
//GridView1.DataSource = dt;
//GridView1.DataBind();
}
解决方案
Hi,
Count the Gridview Rows and then Add the Values by using the counted Rows Values
like the Below code(it C# Windows Application Code)
string dates= textBox1.Text.ToString();
int i=dataGridView1.Rows.Count;
int j=i-1;
this.dataGridView1.Rows.Add();
dataGridView1.Rows[j].Cells[0].Value=dates;
textBox1.Text="";
Thanks
public partial class test : System.Web.UI.Page
{
DataTable tb = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
createtable();
}
protected void Button1_Click(object sender, EventArgs e)
{
tb = (DataTable)ViewState["table1"];
tb.Rows.Add(TextBox1.Text, tb.Rows.Count + 1);
GridView1.DataSource = tb;
GridView1.DataBind();
}
public void createtable()
{
tb.Columns.Add("Name", typeof(string));
tb.Columns.Add("Id", typeof(int));
ViewState["table1"] = tb;
}
}
public partial class test2 : System.Web.UI.Page
{
static DataTable table1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Int32 Rcount =table1.Rows.Count;
if (Rcount == 0)
{
table1.Columns.Add("FName");
table1.Rows.Add(TextBox1.Text);
}
else
{
DataRow dr = table1.NewRow();
dr["FName"] = TextBox1.Text;
table1.Rows.Add(dr);
}
GridView1.DataSource = table1;
GridView1.DataBind();
}
}
这篇关于如何从文本框中读取数据并显示到Gridview中,而不会覆盖Gridview中的现有行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文