DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using APPcarDA;
using APPcarfangfa;

namespace WindowsFormsApplication2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<DA> list = new fangfa().select();
            dataGridView1.AutoGenerateColumns = false;//自动生成列
            dataGridView1.DataSource = list;//自动添加
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show( dataGridView1.SelectedCells[0].Value.ToString());//获取选中的单元格。选中的单元格的集合,最后选中的单元格在最前面
            dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选中的行集合,可以根据选中的行具体到这一行的任一单元格
            DA car=(DA) dataGridView1.SelectedRows[0].DataBoundItem;//将某一行直接转换成对象
            MessageBox.Show(car.Oil.ToString());
        }
    }
}
原文地址:https://www.cnblogs.com/dlexia/p/4618402.html