我从数据表中填充了一个 datagridview。应用程序运行时如何从 datagridview 中读取数据?
Madlinux
问问题
4946 次
4 回答
2
你是如何填充它的?DataSource 是否有用,例如 BindlingList?如果是这样的话:
BindingSource bindingSource = this.dataGridView1.DataSource as BindingSource;
//substitute your business object type for T
T entity = bindingSource.Current as T;
将使您将实体绑定到该行。
否则总会有 datagridview.Columns[n].Cells[n].Value 但我真的会考虑使用 DataSource 中的对象
编辑:啊......一个数据表......对:
var table = dataGridView1.DataSource as DataTable;
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.WriteLine(row[column]);
}
}
于 2008-09-20T21:19:30.980 回答
1
您可以遍历您的 datagridview 并检索每个单元格。
for(int i =0; i < DataGridView.Rows.Count; i++){
DataGridView.Rows.Columns["columnName"].Text= "";
}
这里有一个例子。
于 2008-09-20T21:27:00.273 回答
0
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public static DataTable objDataTable = new DataTable("UpdateAddress");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "csv files (*.csv)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string fileName = openFileDialog1.FileName;
List<string> dataFile = new List<string>();
dataFile = ReadList(fileName);
foreach (string item in dataFile)
{
string[] temp = item.Split(',');
DataRow objDR = objDataTable.NewRow();
objDR["EmployeeID"] = temp[0].ToString();
objDR["Street"] = temp[1].ToString();
objDR["POBox"] = temp[2].ToString();
objDR["City"] = temp[3].ToString();
objDR["State"] = temp[4].ToString();
objDR["Zip"] = temp[5].ToString();
objDR["Country"] = temp[6].ToString();
objDataTable.Rows.Add(objDR);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
public static List<string> ReadList(string filename)
{
List<string> fileData = new List<string>();
StreamReader sr = new StreamReader(filename);
while (!sr.EndOfStream)
fileData.Add(sr.ReadLine());
return fileData;
}
private void Form1_Load(object sender, EventArgs e)
{
objDataTable.Columns.Add("EmployeeID", typeof(int));
objDataTable.Columns.Add("Street", typeof(string));
objDataTable.Columns.Add("POBox", typeof(string));
objDataTable.Columns.Add("City", typeof(string));
objDataTable.Columns.Add("State", typeof(string));
objDataTable.Columns.Add("Zip", typeof(string));
objDataTable.Columns.Add("Country", typeof(string));
objDataTable.Columns.Add("Status", typeof(string));
dataGridView1.DataSource = objDataTable;
dataGridView1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
// Displays a SaveFileDialog so the user can save the backup of AD address before the update
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "BAK Files|*.BAK";
saveFileDialog1.Title = "Save AD Backup";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
TextWriter fileOut = new StreamWriter(saveFileDialog1.FileName);
//This is where I want read from the datagridview the EmployeeID column and use it in my BackupAddress method.
}
}
于 2008-09-20T21:28:06.403 回答
0
您可能想看看DataTable.WriteXml,它是DataTable.ReadXml的兄弟。无需大惊小怪,无需大惊小怪地保存 DataTable。
于 2008-09-21T00:29:49.837 回答