小弟求教 问什么gridControl总是连接不上数据库 (附图)
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Configuration; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //连接字符串 获取配置文件里的连接路径 static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; //查看并刷新所有数据 // private void btnView_Click(object sender, EventArgs e) // { // string sql = "select Id,Movie_Name,Movie_Director,Date_Released from Record"; // SqlConnection conn = new SqlConnection(connStr); // SqlCommand cmd = new SqlCommand(sql, conn); // DataTable dt = new DataTable(); // SqlDataAdapter sda = new SqlDataAdapter(cmd); // sda.Fill(dt); // gridControl1.DataSource = dt; // } //当系统加载的时候自动显示DataGridView的数据 private void Form1_Load(object sender, EventArgs e) { string sql = "select Id,Market_GoodsName,Market_GoodsFactory,Date_Released from Record"; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(sql, conn); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); gridControl1.DataSource = dt; } //将鼠标点击的datagridview 上的行内容传递给 文本框 // private void Form1_CellContentClick(object sender, DataGridViewCellEventArgs e) // { //获取字段名为Movie_Name的单元格内容 // txtName.Text = gridControl1.Rows[e.RowIndex].Cells["Movie_Name"].Value.ToString(); //同理 获取当前点击行里的 name属性为Movie_Director的单元格 获取并将其传至txtDirector 文本框 // txtFactory.Text = gridControl1.Rows[e.RowIndex].Cells["Movie_Director"].Value.ToString(); //new一个时间对象 目的是将电影发行时间的小时,分和秒给去掉 保留到最小单位为日 // DateTime datetoDay = new DateTime().Date; //将当前行的日期单元格的值 赋给 时间对象datetoDay // datetoDay = Convert.ToDateTime(gridControl1.Rows[e.RowIndex].Cells["Date_Released"].Value); //通过ToShortDateString()方法 将日期后的00:00:00 给剔除掉 并赋给 txtDate文本框 // txtDate.Text = datetoDay.ToShortDateString(); // } } }
恳请各位前辈指点