我从数据表中填充了datagridview。如何在应用程序运行时从datagridview读取?

有帮助吗?

解决方案

你是如何填充它的? 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]);
     }
 }

其他提示

您可以遍历数据网格视图并检索每个单元格。

for(int i =0; i < DataGridView.Rows.Count; i++){
  DataGridView.Rows.Columns["columnName"].Text= "";
} 

此处有一个示例。

您可能需要查看 DataTable .WriteXml ,它的兄弟 DataTable。的ReadXml 。没有大惊小怪,没有保存数据表。

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.
            }

        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top