我现在想要的是把从datagridview中一行查询出来的数据当做一个对象,然后根据对象来取值
而不是这样:
textBox1.Text = ds.Tables[0].Rows[0][1].ToString();
textBox2.Text = ds.Tables[0].Rows[0][0].ToString();
这样就有两个对象,不是我想要的,多多提点意见。
而不是这样:
textBox1.Text = ds.Tables[0].Rows[0][1].ToString();
textBox2.Text = ds.Tables[0].Rows[0][0].ToString();
这样就有两个对象,不是我想要的,多多提点意见。
如
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow row = dataGridView1.SelectedRows[0];
MessageBox.Show(row.Cells[0].Value.ToString());
}
}要么自己做一个类,
先构造类对象,然后再用。
如:
public class Class1
{
public string s;
public Class1(string ss)
{
this.s = ss; }
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
Class1 c = new Class1(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
MessageBox.Show(c.s);
} }
但是如果我是要用TEXTBOX来显示数据捏?
/// 新建类
/// </summary>
class DgvTest
{
public int Id { get; set; }
public string Name { get; set; }
}
/// <summary>
/// 示例方法
/// </summary>
private void TestDgv()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
var item = row.DataBoundItem as DgvTest; //将DataGridView中每一行作为一个对象
int id = item.Id; //取对象中的属性
string name = item.Name;
}
}楼主是否是这个意思?你可以参考,这个是自己建立个需要的类,将每行转换为该类,然后对其中的需要的属性取值