只要东西好 分还可以加!!
求各位大哥大姐 拿出来分享下!!
求各位大哥大姐 拿出来分享下!!
解决方案 »
- messagequeue 返回对象
- 在WinFrom开发中使用删除Ribbon控件是出现“将对象设置引用到对象的实例”错误
- c#创建动态网页,可打开后网页无内容
- 在LISTBOX中添加对象更新的问题
- 在DataGridView中添加自动汇总行
- 急!!Vista下 用VS2003 无法创建ASP.NET 项目
- 求一段代码,简单链表结构
- 请问下面这些东西怎么转换成中文
- 我如何定义一个string的派生类xxx
- C#调用Matlab程序成功执行却无法将数据写入Excel
- 。。。在智能设备上开发,结果FileStream的第一个参数只能传递path字符串,不支持IntPtr,咋办?
- 新手问下数据库更新的问题
慢慢看
我写的例子 一个窗体 2个控件 一个DataGridView 一个Timer private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = Init();//绑定数据源的方法 返回一个实体类的对象的数组或泛型
this.timer1.Interval = 3000;//变色时间 单位毫秒
this.timer1.Start();
} private void ChageColor(int RowIndex)//传变色的行序号
{
if (RowIndex < this.dataGridView1.Rows.Count && RowIndex >=0)
{
this.dataGridView1.Rows[RowIndex].DefaultCellStyle.ForeColor = Color.Red; //可以自己设置要变的颜色
}
} private void timer1_Tick(object sender, EventArgs e)
{
ChageColor(0);//变色方法
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sid = GridView1.Rows[e.RowIndex].Cells[0].Text; SqlConnection conn = com.GetConn();
SqlCommand comm = new SqlCommand("delete from 表名 where id=@id", conn);
comm.Parameters.AddWithValue("@id", sid); conn.Open();
comm.ExecuteNonQuery();
conn.Close();
//刷新页面
setdata();} void setdata()
{
SqlConnection conn = com.GetConn();
SqlCommand comm = new SqlCommand("select * from 表名", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable table = new DataTable();
da.Fill(table); GridView1.DataSource = table;
GridView1.DataBind();
}
那些事件是在RowEditing(这个是修改的事件,要是修改 就在这里编写) 删除的事件是在RowDeleting 中编写的
这里面全有了 好好看看吧
datagridview导出为excel文件的C#实例
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;namespace AssetsManage.Operation
{
class Export
{
private static System.Windows.Forms.DataGridView gridView;
private static System.Windows.Forms.ToolStripProgressBar toolStripProgressBar1;
private static Timer time;
private static DataSet objSet = new DataSet();
private static SaveFileDialog saveFileDialog = new SaveFileDialog();
private static SaveFileDialog saveFileDialog2 = new SaveFileDialog();
public static System.Windows.Forms.DataGridView _gridView
{
get { return gridView; }
set { gridView = value;}
}
public static System.Windows.Forms.ToolStripProgressBar _toolStripProgressBar1
{
get { return toolStripProgressBar1; }
set { toolStripProgressBar1 = value;}
}
public static Timer _time
{
get { return time; }
set { time = value;}
}
public static DataSet _objSet
{
get { return objSet; }
set { objSet.Clear(); objSet = value; }
} //导出当前页DataGridView中的数据到EXcel中
public static void ExportTOExcel()
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
toolStripProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test"; //生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + gridView[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
toolStripProgressBar1.Value += 100 / gridView.RowCount;
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;
}
}
} //-------------------------------------------------------------------------------------------------------------------------------------
//导出整个DataGridView中的数据到Excel中
public static void ExportTOExcel2()
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog2.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog2.FilterIndex = 0;
saveFileDialog2.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
saveFileDialog2.Title = "导出文件保存路径";
saveFileDialog2.FileName = null;
saveFileDialog2.ShowDialog();
string FileName = saveFileDialog2.FileName; if (FileName.Length != 0)
{
toolStripProgressBar1.Visible = true;
System.Data.DataTable dt = objSet.Tables[0]; FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
toolStripProgressBar1.Value = 0; for (int i = 0; i < dt.Columns.Count; i++)
{
strLine = strLine + dt.Columns[i].ColumnName.ToString() + Convert.ToChar(9); }
objStreamWriter.WriteLine(strLine);
strLine = ""; for (int i = 0; i < dt.Rows.Count; i++)
{
strLine = strLine + (i + 1) + Convert.ToChar(9);
for (int j = 1; j < dt.Columns.Count; j++)
{
strLine = strLine + dt.Rows[i][j].ToString() + Convert.ToChar(9); }
objStreamWriter.WriteLine(strLine);
toolStripProgressBar1.Value += 100 / dt.Rows.Count;
strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog2.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;
}
}
} //————————————————————————————————————————————————————————————————————
//导出到XML(整个数据源) public static void ExportTOXML()
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog2.Filter = "XML files (*.xml)|*.xml";
saveFileDialog2.FilterIndex = 0;
saveFileDialog2.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
saveFileDialog2.Title = "导出文件保存路径";
saveFileDialog2.FileName = null;
saveFileDialog2.ShowDialog();
string FileName = saveFileDialog2.FileName; if (FileName.Length != 0)
{
toolStripProgressBar1.Visible = true;
objSet.WriteXml(saveFileDialog2.FileName.ToString());
for (int i = 0; i < objSet.Tables[0].Rows.Count; i++)
{
toolStripProgressBar1.Value += 100 / objSet.Tables[0].Rows.Count;
}
MessageBox.Show("数据已经成功导出到:" + saveFileDialog2.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;
}
}
}
}
}
在datagridview的数据修改后,点保存,更新数据库
DataSet ds = new DataSet();
SqlDataAdapter sda;SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];
update 表 set 列2=datagridview.rows[i][1].toString(),列3=datagridview.rows[i].cell[2].toString() where ID = '"+datagridview.row[i].cell[0]+"';
写一个循环,如果checkbox为true,就执行更新。
刚写的一个日期转换的类,看着给分吧
!!!!!!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
label1.Text = LunDay.GetLunarCalendar(dateTimePicker1.Value); label1.Text += "\n" + LunDay.GetTianGanDiZhi(dateTimePicker1.Value);
label1.Text += "\n" + LunDay.GetMonthDay(dateTimePicker1.Value);
label1.Text += "\n" + LunDay.GetWeekDay(dateTimePicker1.Value);
label1.Text += "\n" + LunDay.GetShengXiao(dateTimePicker1.Value);
} private void button2_Click(object sender, EventArgs e)
{
List<list> lst = new List<list>();
lst.Add(new list("zhan","hebei"));
lst.Add(new list("li","zhanjiakou"));
comboBox1.DataSource = lst;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "address";
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
} private void button3_Click(object sender, EventArgs e)
{
List<list> lst = new List<list>();
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "address";
lst.Add(new list("wang", "beijin"));
lst.Add(new list("de", "tianji"));
comboBox1.DataSource = lst;
}
}
}
来实现!
给个例子 最好~