读取数据网格视图

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

请先 登录 后评论
本文连接: http://www.china-sunrider.com.cn/question/11196
source: https://stackoverflow.com/questions/109417

4 个回答

Hamish Smith

你是如何填充它的? 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 但实际上我会考虑使用数据源中的对象

编辑:啊...数据表...righto:

 var table = dataGridView1.DataSource as DataTable;

 foreach(DataRow row in table.Rows)
 {
     foreach(DataColumn column in table.Columns)
     {
         Console.WriteLine(row[column]);
     }
 }
请先 登录 后评论
jdecuyper

您可以迭代 datagridview 并检索每个单元格。

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

有一个示例

请先 登录 后评论
stackoverflow_user
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.
            }

        }
请先 登录 后评论
Mark Brackett

您可能想查看DataTable .WriteXml,它的兄弟DataTable。 ReadXml。无需大惊小怪,无需麻烦地保存数据表。

请先 登录 后评论
user contributions licensed under CC BY-SA.