将几个文本框数据添加到datagridview c# [英] add several textbox data to datagridview c#

查看:219
本文介绍了将几个文本框数据添加到datagridview c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有文本框数据添加到datagridview我是c#.net中的新用户,即按以下步骤操作:

i want to add all text box data to datagridview i am new in c#.net, i m doing as follows,

dataGridView1.Rows[1].Cells[1].Value = textBox1.Text;
dataGridView1.Rows[2].Cells[2].Value = textBox2.Text;
dataGridView1.Rows[3].Cells[3].Value = textBox3.Text;
dataGridView1.Rows[4].Cells[4].Value = textBox4.Text;





但是我想为此使用循环,但问题是如何通过重新编号tn文本框名称来后缀i,即如何替换1 in textbox1.text by textbox [i] .text (textbox + i +)。text

请帮助



but i want to use loop for this but problem is how to suffix i by repalcing number tn textbox name i.e how to replace 1 in textbox1.text by textbox[i].text or (textbox+i+).text
please help

for(int i=0;i<4;i++)
{
    dataGridView1.Rows[i].Cells[i].Value = textBox[i].Text;
}

推荐答案

Step1 :在面板中添加所有文本框。以下是代码:



在ASPX中

Step1: Add all textboxes inside a panel. Here is the code:

In ASPX
<asp:panel id="pnlTextBoxes" runat="server" xmlns:asp="#unknown">
	<asp:textbox id="textBox1" runat="server" />
	<asp:textbox id="textBox2" runat="server" />
	<asp:textbox id="textBox3" runat="server" />
	<asp:textbox id="textBox4" runat="server" />
</asp:panel>



Step2 :在此步骤中,您需要在* .aspx.cs页面中添加代码。以下是代码:



代码隐藏


Step2: In this step you need to add code in your *.aspx.cs page. Here is the code:

In Code-behind

for(int i=1;i<=4;i++)
{
	TextBox txtInstance = (TextBox)pnlTextBoxes.FindControl("textBox" + i);
	if(txtInstance != null)
	{
		dataGridView1.Rows[i].Cells[i].Value = txtInstance.Text;
	}
}


var txts = new List<TextBox>(){ textBox1, textBox2, textBox3, textBox4 };
for(int i=0;i<3;i++)//if i=0 then it must be <3
{
    dataGridView1.Rows[i].Cells[i].Value = txts[i].Text;
}





-KR



-KR


这篇关于将几个文本框数据添加到datagridview c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆