C# DataGridView手动添加数据

来源:互联网 发布:港澳台直播软件apk 编辑:程序博客网 时间:2024/05/15 11:10

开发环境:VS2008

.Net版本:.Net Framework 2.0

最终效果图片:

源码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace美化

{

publicpartialclassForm1 :Form

   {

public Form1()

       {

           InitializeComponent();

       }

 

privatevoid button2_Click(object sender,EventArgs e)

       {

 

DataGridViewRow row =newDataGridViewRow();

 

int index= dataGridView1.Rows.Add(row);

           dataGridView1.Rows[index].Cells[0].Value = textBox1.Text;

           dataGridView1.Rows[index].Cells[1].Value = textBox2.Text;

           dataGridView1.Rows[index].Cells[2].Value = textBox3.Text;

           dataGridView1.Rows[index].Cells[3].Value = textBox4.Text;

           dataGridView1.Rows[index].Cells[4].Value = textBox5.Text;

           dataGridView1.Rows[index].Cells[5].Value = Image.FromFile(textBox6.Text);

 

       }

 

privatevoid Form1_Load(object sender,EventArgs e)

       {

 

DataGridViewTextBoxColumn col =newDataGridViewTextBoxColumn();

DataGridViewTextBoxColumn col1 =newDataGridViewTextBoxColumn();

DataGridViewTextBoxColumn col2 =newDataGridViewTextBoxColumn();

DataGridViewTextBoxColumn col3 =newDataGridViewTextBoxColumn();

DataGridViewTextBoxColumn col4 =newDataGridViewTextBoxColumn();

DataGridViewImageColumn col5 =newDataGridViewImageColumn();

 

DataGridViewTextBoxCell celltext =newDataGridViewTextBoxCell();

DataGridViewImageCell cellimage =newDataGridViewImageCell();

 

 

           col.CellTemplate = col1.CellTemplate = col2.CellTemplate = col3.CellTemplate = col4.CellTemplate = celltext;

           col5.CellTemplate = cellimage;

           col.HeaderText = "名称";

           col1.HeaderText = "分类";

           col2.HeaderText = "数量";

           col3.HeaderText = "价格";

           col4.HeaderText = "销售时间";

           col5.HeaderText = "图片";

 

           dataGridView1.Columns.Add(col);

           dataGridView1.Columns.Add(col1);

           dataGridView1.Columns.Add(col2);

           dataGridView1.Columns.Add(col3);

           dataGridView1.Columns.Add(col4);

           dataGridView1.Columns.Add(col5);

 

 

 

       }

 

privatevoid button1_Click(object sender,EventArgs e)

       {

           openFileDialog1.Filter = "png图像文件|*.png";

if(openFileDialog1.ShowDialog()==DialogResult.OK)

           {

               textBox6.Text =openFileDialog1.FileName;   

 

 

           }

       }

 

 

privatevoid button3_Click(object sender,EventArgs e)

       {

           monthCalendar1.Visible = true;

       }

 

privatevoid monthCalendar1_DateSelected(object sender,DateRangeEventArgs e)

       {

           textBox5.Text = monthCalendar1.SelectionStart.ToLongDateString();

           monthCalendar1.Visible = false;

       }

   }

}

 

 

0 0
原创粉丝点击