1. 程式人生 > >.NET使用flexcell控制元件(2)

.NET使用flexcell控制元件(2)

1.新增資料

//新增一行資料
grid1.Rows += 1;
int row = grid1.Rows-1;
string sid, sname,sclass,smajor, ssex;
int sage;
//獲取新建行的資料
sid = grid1.Cell(row, 2).Text;
sname = grid1.Cell(row, 3).Text;
sclass = grid1.Cell(row,4).Text;
smajor = grid1.Cell(row, 5).Text;           
ssex = grid1.Cell(row, 6).Text;
sage = int.Parse(grid1.Cell(row, 7).Text);

//新增新資料到資料庫            
string sql = string.Format("insert into user_info values('{0}','{1}','{2}','{3}','{4}',{5})",sid, sname,sclass,smajor, ssex, sage);

string datasource = @"";
SqlConnection con = new SqlConnection(datasource);
con.Open();

SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();

MessageBox.Show("新增成功!");

2.刪除資料

//刪除某一行資料
int index = grid1.Selection.FirstRow;//獲取選中刪除行的行號
if(index!=0)
{
    int ii = int.Parse(grid1.Cell(index, 1).Text);//獲取刪除行資料id值
     DialogResult result = MessageBox.Show("確認刪除嗎?", "刪除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
     if (result == DialogResult.Yes)
     {
         //刪除表格資料
         grid1.RemoveItem(index);
         //刪除資料庫中資料
         data();
         string mysql = string.Format("delete user_info where id={0}", ii);
         SqlCommand cmd = new SqlCommand(mysql, con);
         cmd.ExecuteNonQuery();
         con.Close();
      }
      else
      {
           return;
      }
}
else
{
   MessageBox.Show("請選擇刪除項");
}

3.修改資料

//修改某一行資料
int index = grid1.Selection.FirstRow;//獲取修改行行號
if(index!=0) 
{
      int ii = int.Parse(grid1.Cell(index, 1).Text);//獲取修改行資料id值
      data();
      string sql = string.Format("select * from user_info where id={0}", ii);
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = new SqlCommand(sql, con);
      SqlCommandBuilder scb = new SqlCommandBuilder(adapter);
      DataSet ds = new DataSet();
      adapter.Fill(ds);
      DataRow row = ds.Tables[0].Rows[0];
      row[1] = grid1.Cell(index, 2).Text;          
      row[2] = grid1.Cell(index, 3).Text;
      row[3] = grid1.Cell(index, 4).Text;
      row[4] = grid1.Cell(index, 5).Text;
      row[5] = grid1.Cell(index, 6).Text;
      row[6] = grid1.Cell(index, 7).Text;
      adapter.Update(ds);
      con.Close();
      MessageBox.Show("修改成功");
}
else
{
   MessageBox.Show("請選擇修改項");
}

4.查詢資料

//查詢姓名(name)包含tb1.Text文字的資料
string datasource = @"";
SqlConnection con = new SqlConnection(datasource);
con.Open();
 
string mysql = string.Format("select * from user_info where name like '%{0}%'",tb1.Text);
 
DataTable dtable = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(mysql, con);
adapter.Fill(dtable);
 
grid1.Rows = dtable.Rows.Count + 1;
int i = 0;
foreach(DataRow dr in dtable.Rows)
{
    i++;
    grid1.Cell(i, 1).Text = dr["id"].ToString();
    grid1.Cell(i, 2).Text = dr["stu_id"].ToString();
    grid1.Cell(i, 3).Text = dr["name"].ToString();
    grid1.Cell(i, 4).Text = dr["sclass"].ToString();
    grid1.Cell(i, 5).Text = dr["smajor"].ToString();
    grid1.Cell(i, 6).Text = dr["ssex"].ToString();
    grid1.Cell(i, 7).Text = dr["sage"].ToString();
}

5.匯入資料

//InportExcelTableByOleDB()方法
public static DataTable InportExcelTableByOleDB(string excelpath)
{
     try
     {
          DataTable dtExcel = new DataTable();
          DataSet ds = new DataSet();
          OleDbConnection objConn = null;
          objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelpath + ";" + "Extended Properties=\"Excel 8.0;HDR=yes;IMEX=1;\"");
          if (objConn == null)
          {
              return null;
          }
          objConn.Open();
          DataTable table = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
          string sheetname = table.Rows[0]["TABLE_NAME"].ToString();
          string sql = "select * from [" + sheetname + "]";
          OleDbDataAdapter oda = new OleDbDataAdapter(sql, objConn);
          oda.Fill(ds, sheetname);
          objConn.Close();
          dtExcel = ds.Tables[sheetname];
          return dtExcel;
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
          return null;
     }
     
}
//匯入事件實現,呼叫InportExcelTableByOleDB()方法
private void grid1_inport_Click(object sender, EventArgs e)
{
      inportExcel.Filter = "Excel檔案|*.xls";
      inportExcel.ShowDialog();
      string filepath = inportExcel.FileName.Trim();
      if (filepath == "") return;
      if (!File.Exists(filepath)) return;
           
      try
      {
          DataTable dtExcel = InportExcelTableByOleDB(filepath);

          if (dtExcel.Rows.Count > 0)
          {
               grid1.Rows = dtExcel.Rows.Count + 1;
               int i = 0;
               for (int j = 0; j < dtExcel.Rows.Count; j++)
               {
                    i++;
                    grid1.Cell(i, 2).Text = dtExcel["stu_id"].ToString();
                    grid1.Cell(i, 3).Text = dtExcel["name"].ToString();
                    grid1.Cell(i, 4).Text = dtExcel["sclass"].ToString();
                    grid1.Cell(i, 5).Text = dtExcel["smajor"].ToString();
                    grid1.Cell(i, 6).Text = dtExcel["ssex"].ToString();
                    grid1.Cell(i, 7).Text = dtExcel["sage"].ToString();

                }
            }
      }
      catch (Exception ex)
      {
           MessageBox.Show("資料匯入錯誤,請重新匯入");
      }
}

6.匯出資料到Excel

ExportToExcel方法

object.ExportToExcel (FileName, ExportFixedRow, ExportFixedCol)object.ExportToExcel (FileName, SheetName, ExportFixedRow, ExportFixedCol)

ExportToExcel 方法的語法包含如下部分:

部分 描述
object Grid控制元件。
FileName 一個String值,用於指定檔名。
FileName 一個String值,用於指定Sheet的名稱。
ExportFixedRow 可選的。一個Boolean值,該值決定是否輸出固定行。
ExportFixedCol 可選的。一個Boolean值,該值決定是否輸出固定列。
grid1.ExportToExcel("", true, true);