如何将datagridview行中的多个值附加到字符串varialbe [英] how can i append more than one value from datagridview rows to a string varialbe

查看:59
本文介绍了如何将datagridview行中的多个值附加到字符串varialbe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用for循环将来自datagridview行的多个值附加到字符串varialbe或单个文本框。

how can i append more than one value from datagridview rows to a string varialbe or single textbox using for loop.

推荐答案

这样做并不困难。你需要做的就是遍历行集合;)



WinForm:

http://msdn.microsoft.com/en-US/library/system.windows.forms.datagridview.rows。 aspx [ ^ ]



WebControl:

http://msdn.microsoft.com/en-US/library/system.web.ui.webcontrols.gridview.rows.aspx [ ^ ]

http://forums.asp.net/t/ 1662914.aspx / 1 [ ^ ]



试试!
It's not so hard to do it. All you need to do is to loop through the rows collection ;)

WinForm:
http://msdn.microsoft.com/en-US/library/system.windows.forms.datagridview.rows.aspx[^]

WebControl:
http://msdn.microsoft.com/en-US/library/system.web.ui.webcontrols.gridview.rows.aspx[^]
http://forums.asp.net/t/1662914.aspx/1[^]

Try!


//使用Windows应用程序并添加ID为dataGridView1的DataGridView。

//按下按钮并在其点击事件下面放置代码。

//希望它有所帮助。 :) .... Prasad





DataTable dt;



private void Form1_Load(object sender,EventArgs e)

{

dt = new DataTable();

dt .Columns.Add(Col1);

dt.Columns.Add(Col2);

dt.Columns.Add(Col3);

dt.Columns.Add(Col4);



DataRow dr = dt.NewRow();

dr [Col1] =val1;

dr [Col2] =val2;

dr [Col3] =val3;

dr [Col4] =val4;



dt.Rows.Add(dr);



dataGridView1.DataSource = dt;

}



private void button1_Click(object sender,EventArgs e)

{

string temp;



for(int i = 0; i< dataGrid View1.Rows.Count; i ++)

{

//显示所有值

MessageBox.Show(Convert.ToString(

dataGridView1) .Rows [i] .Cells [col1]。Value +\ n+

dataGridView1.Rows [i] .Cells [col2]。Value +\ n +

dataGridView1.Rows [i] .Cells [col3]。Value)

);

//获得2个值单个字符串

temp = dataGridView1.Rows [i] .Cells [col1]。值+

dataGridView1.Rows [i] .Cells [col2]。价值;



}

}
// Take Windows application and add DataGridView with id "dataGridView1".
// Take button and place below code on its click event.
// Hope it helps. :) ....Prasad


DataTable dt;

private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Columns.Add("Col3");
dt.Columns.Add("Col4");

DataRow dr = dt.NewRow();
dr["Col1"] = "val1";
dr["Col2"] = "val2";
dr["Col3"] = "val3";
dr["Col4"] = "val4";

dt.Rows.Add(dr);

dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
string temp;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
// To show all values
MessageBox.Show(Convert.ToString(
dataGridView1.Rows[i].Cells["col1"].Value + "\n" +
dataGridView1.Rows[i].Cells["col2"].Value + "\n" +
dataGridView1.Rows[i].Cells["col3"].Value)
);
// To get 2 values in single string
temp = dataGridView1.Rows[i].Cells["col1"].Value +
dataGridView1.Rows[i].Cells["col2"].Value;

}
}


这篇关于如何将datagridview行中的多个值附加到字符串varialbe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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