我是靠谱客的博主 秀丽外套,最近开发中收集的这篇文章主要介绍DataGridView初始化——添加列、添加行号、连接数据库、单击单元格在文本框中显示,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

<pre name="code" class="csharp"> /// <summary>
        /// 单击DataGridView中的单元格,在文本框中显示单击的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex>0)
            {
                txtName.Text = dgvManager.CurrentRow.Cells[2].Value.ToString();
                txtDirector.Text = dgvManager.Rows[e.RowIndex].Cells["Movie_Director"].Value.ToString();
                DateTime datetoDay = new DateTime().Date;                                               //new一个时间对象 目的是将电影发行时间的小时,分和秒给去掉 保留到最小单位为日
                datetoDay = Convert.ToDateTime(dgvManager.Rows[e.RowIndex].Cells["Data_Released"].FormattedValue);//将当前行的日期单元格的值 赋给 时间对象datetoDay           
                txtRelease.Text = datetoDay.ToShortDateString();                                         //通过ToShortDateString()方法 将日期后的00:00:00 给剔除掉 并赋给 txtDate文本框
                label5.Text = "ID:" + dgvManager.Rows[e.RowIndex].Cells["ID"].Value.ToString();

            }
        }
<pre name="code" class="csharp">/// <summary> 
/// 为DataGridView添加列 
/// </summary> 
/// <param name="dgv">形参,传入创建的dgv对象</param> 
/// <param name="column">列对象</param> /// <param name="dgvReadonly">dgv只读属性</param> 
/// <param name="dgvName">dgv列名称,最好与数据库相应的名称相同</param> 
/// <param name="dgvDatapropertyname">dgv对应数据库名称</param>
 /// <param name="dgvHeadertext">显示的列名</param> 
public void AddDgvColumns(DataGridView dgv, DataGridViewColumn column, bool dgvReadonly, string dgvName, string dgvDatapropertyname, string dgvHeadertext, int dgvWidth, bool dgvVisible) 
{ 
column.ReadOnly = dgvReadonly; 
column.DataPropertyName = dgvDatapropertyname; 
column.Name = dgvName; 
column.HeaderText = dgvHeadertext; 
column.Width = dgvWidth; 
column.Visible = dgvVisible; 
dgv.Columns.Add(column); 
}


 
 
  /// <summary>
        /// 初始化DataGridView
        /// </summary>
        /// <param name="dgv"></param>
        public void DgvInitial(DataGridView dgv)
        {
            if (dgv != null && dgv.ColumnCount > 0)
            {
                dgv.Columns.Clear();
            }
            dgv.AllowUserToAddRows = true;
            dgv.AllowUserToDeleteRows = true;
            dgv.AllowUserToResizeColumns = true;
            dgv.AllowUserToOrderColumns = true;

            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.Width = 30;//DataGridViewCheckBoxColumn 承载 DataGridViewCheckBoxCell 对象的集合
            dgv.Columns.Add(checkBoxColumn);

            this.AddDgvColumns(dgv, new DataGridViewTextBoxColumn(), true, "ID", "ID", "编号", 60, true);
            this.AddDgvColumns(dgv, new DataGridViewTextBoxColumn(), true, "Movie_Name", "Movie_Name", "电影", 80, true);
            this.AddDgvColumns(dgv, new DataGridViewTextBoxColumn(), true, "Movie_Director", "Movie_Director", "导演", 80, true);
            this.AddDgvColumns(dgv, new DataGridViewTextBoxColumn(), true, "Data_Released", "Data_Released", "发行日期", 80, true);
            this.AddDgvColumns(dgv, new DataGridViewTextBoxColumn(), true, "UserType", "UserType", "用户类型", 80, true);


        /// <summary>
        /// 为表格添加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="GVmain"></param>
        public void AddGridView_HangHao(object sender, DataGridViewRowPostPaintEventArgs e, DataGridView GVmain)
        {
            try
            {                //添加行号                
                SolidBrush v_SolidBrush = new SolidBrush(GVmain.RowHeadersDefaultCellStyle.ForeColor);//设置行标题默认样式的前景色
                int v_LineNo = 0;
                v_LineNo = e.RowIndex + 1;
                string v_Line = v_LineNo.ToString();
                e.Graphics.DrawString(
                    v_Line,
                    e.InheritedRowStyle.Font,
                    v_SolidBrush,
                    e.RowBounds.Location.X + 15,
                    e.RowBounds.Location.Y + 5);
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败");
            }
        }
        /// 连接数据库
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable dgvDataSource(string sql)
        {
            try
            {
                
                string connstr = "server=.;database=movie;Integrated Security=true;";
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                int n = cmd.ExecuteNonQuery();
                conn.Close();
                return dt;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} has errow!", e);
                return null;
            }
        }


http://www.cnblogs.com/peterzb/archive/2009/05/29/1491891.html


最后

以上就是秀丽外套为你收集整理的DataGridView初始化——添加列、添加行号、连接数据库、单击单元格在文本框中显示的全部内容,希望文章能够帮你解决DataGridView初始化——添加列、添加行号、连接数据库、单击单元格在文本框中显示所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部