1.怎样定制VC#DataGrid列标题?

  DataGridTableStyle dgts = new DataGridTableStyle();

  dgts.MappingName = "myTable"; //myTable为要载入数据的DataTable 

  DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();

  dgcs.MappingName = "title_id";

  dgcs.HeaderText = "标题ID";

  dgts.GridColumnStyles.Add(dgcs);

  。。。

  dataGrid1.TableStyles.Add(dgts);

2.检索某个字段为空的所有记录的条件语句怎么写?

  ...where col_name is null

3.如何在c# Winform应用中接收回车键输入?

  设一下form的AcceptButton.

4.比如Oracle中的NUMBER(15),在Sql Server中应是什么?

  NUMBER(15):用numeric,精度15试试。

5.sql server的应用like语句的存储过程怎样写?

  select * from mytable where haoma like ‘%’ + @hao + ‘%’

6.vc# winform中如何让textBox接受回车键消息(假没没有按钮的情况下)?

  private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

  {

  if(e.KeyChar != (char)13)

  return;

  else

  //do something;

  }

7.为什么(Int32)cmd.ExecuteScalar()赋值给Int32变量时提示转换无效?

  Int32.Parse(cmd.ExecuteScalar().ToString());

8.DataSource为子表的DataGrid里怎样增加一个列以显示母表中的某个字段?

  在子表里手动添加一个列。

  DataColumn dc = new DataColumn("newCol", Type.GetType("System.String"));
  dc.Expression = "Parent.parentColumnName";

  dt.Columns.Add(dc); //dt为子表

9.怎样使DataGrid显示DataTable中某列的数据时只显示某一部分?

  select ..., SUBSTR(string, start_index, end_index) as ***, *** from ***

10.如何让winform的combobox只能选不能输入?

  DropDownStyle 属性确定用户能否在文本部分中输入新值以及列表部分是否总显示。

  值:

  DropDown --- 文本部分可编辑。用户必须单击箭头按钮来显示列表部分。

  DropDownList --- 用户不能直接编辑文本部分。用户必须单击箭头按钮来显示列表部分。

  Simple --- 文本部分可编辑。列表部分总可见。

11.怎样使winform的DataGrid里显示的日期只显示年月日部分,去掉时间?

  sql语句里加上to_date(日期字段,'yyyy-mm-dd')

12.怎样把数据库表的二个列合并成一个列Fill进DataSet里?

  dcChehao = new DataColumn("newColumnName", typeof(string));

  dcChehao.Expression = "columnName1+columnName2";

  dt.Columns.Add(dcChehao);

  Oracle:

  select col1||col2 from table

  sql server:

  select col1+col2 from table

13.如何从合并后的字段里提取出括号内的文字作为DataGrid或其它绑定控件的显示内容?即把合并后的字段内容里的左括号(和右括号)之间的文字提取出来。

  Select COL1,COL2, case

  when COL3 like ‘%(%’ THEN substr(COL3, INSTR(COL3, ‘(’ )+1, INSTR(COL3,‘)’)-INSTR(COL3,‘(’)-1)

  end as COL3

  from MY_TABLE

14.当用鼠标滚轮浏览DataGrid数据超过一定范围DataGrid会失去焦点。怎样解决?

  this.dataGrid1.MouseWheel+=new MouseEventHandler(dataGrid1_MouseWheel);

  private void dataGrid1_MouseWheel(object sender, MouseEventArgs e)

  {

  this.dataGrid1.Select();

  }

15.怎样把键盘输入的‘+’符号变成‘A’?

  textBox的KeyPress事件中

  if(e.KeyChar == '+')

  {

  SendKeys.Send("A");

  e.Handled = true;

  }

16.怎样使Winform启动时直接最大化?

  this.WindowState = FormWindowState.Maximized;

17.c#怎样获取当前日期及时间,在sql语句里又是什么?

  c#: DateTime.Now

  sql server: GetDate()

18.怎样访问winform DataGrid的某一行某一列,或每一行每一列?

  dataGrid[row,col]

19.怎样为DataTable进行汇总,比如DataTable的某列值‘延吉'的列为多少?

  dt.Select("城市='延吉'").Length;

20.DataGrid数据导出到Excel后0212等会变成212。怎样使它导出后继续显示为0212?

  range.NumberFormat = "0000";

  21.

  ① 怎样把DataGrid的数据导出到Excel以供打印?

  ② 之前已经为DataGrid设置了TableStyle,即自定义了列标题和要显示的列,如果想以自定义的视图导出数据该怎么办?

  ③ 把数据导出到Excel后,怎样为它设置边框啊?

  ④ 怎样使从DataGrid导出到Excel的某个列居中对齐?

  ⑤ 数据从DataGrid导出到Excel后,怎样使标题行在打印时出现在每一页?

  ⑥ DataGrid数据导出到Excel后打印时每一页显示’当前页/共几页’,怎样实现?

  ①

  private void button1_Click(object sender, System.EventArgs e)

  {

  int row_index, col_index;
  row_index = 1;
         col_index = 1;

  Excel.ApplicationClass excel = new Excel.ApplicationClass();

  excel.Workbooks.Add(true);  

  DataTable dt = ds.Tables["table"];

  foreach(DataColumn dcHeader in dt.Columns)

  excel.Cells[row_index, col_index++] = dcHeader.ColumnName;

  foreach(DataRow dr in dt.Rows)

  {

  col_index = 0;

  foreach(DataColumn dc in dt.Columns)

  {

  excel.Cells[row_index+1, col_index+1] = dr[dc];

  col_index++;

  }

  row_index++;

  }

  excel.Visible = true;
  }  

  private void Form1_Load(object sender, System.EventArgs e)

  {

  SqlConnection conn = new SqlConnection("server=tao; uid=sa; pwd=; database=pubs");

  conn.Open();  

  SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);

  ds = new DataSet();

  da.Fill(ds, "table");  

  dataGrid1.DataSource = ds;

  dataGrid1.DataMember = "table";

  }

  ②dataGrid1.TableStyles[0].GridColumnStyles[index].HeaderText; //index可以从0~dataGrid1.TableStyles[0].GridColumnStyles.Count遍历。

  ③ Excel.Range range;

  range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Count]);

  range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;

  ④ range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

  ⑤ worksheet.PageSetup.PrintTitleRows = "$1:$1";

  ⑥ worksheet.PageSetup.CenterFooter = "第&P页 / 共&N页";

22.当把DataGrid的Cell内容赋值到Excel的过程中想在DataGrid的CaptionText上显示进度,但不显示。WHY?

  ...

  dataGrid1.CaptionText = "正在导出:" + (row + 1) + "/" + row_cnt;

  System.Windows.Forms.Application.DoEvents();

  ...

  处理当前在消息队列中的所有Windows消息。

  当运行Windows窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。在代码处理事件时,应用程序并不响应。如果在代码中调用DoEvents,则应用程序可以处理其他事件。

  如果从代码中移除DoEvents,那么在按钮的单机事件处理程序执行结束以前,窗体不会重新绘制。通常在循环中使用该方法来处理消息。

23.怎样从Flash调用外部程序,如一个C#编译后生成的.exe?

  fscommand("exec", "应用程序.exe");

  ① 必须把flash发布为.exe

  ② 必须在flash生成的.exe文件所在目录建一个名为fscommand的子目录,并把要调用的可执行程序拷贝到那里。

24.有没有办法用代码控制DataGrid的上下、左右的滚动?

  dataGrid1.Select();

  SendKeys.Send("{PGUP}");

  SendKeys.Send("{PGDN}");

  SendKeys.Send("{^{LEFT}"); // Ctrl+左方向键

  SendKeys.Send("{^{RIGHT}"); // Ctrl+右方向键

25.怎样使两个DataGrid绑定两个主从关系的表?

  DataGrid1.DataSource = ds;

  DataGrid1.DataMember = "母表";

  ...

  DataGrid2.DataSouce = ds;

  DataGrid2.DataMember = "母表.关系名";

26.assembly的版本号怎样才能自动生成?特别是在Console下没有通过VStudio环境编写程序时。

  关键是AssemblyInfo.cs里的[assembly: AssemblyVersion("1.0.*")],命令行编译时包含AssemblyInfo.cs

27.怎样建立一个Shared Assembly?

  用sn.exe生成一个Strong Name:keyfile.sn,放在源程序目录下

  在项目的AssemblyInfo.cs里[assembly: AssemblyKeyFile("..\\..\\keyfile.sn")]

  生成dll后,用gacutil /i myDll.dll放进Global Assembly Cach.

28.在Oracle里如何取得某字段第一个字母为大写英文A~Z之间的记录?

  select * from table where ascii(substr(字段,1,1)) between ascii('A') and ascii('Z')

29.怎样取得当前Assembly的版本号?

  Process current = Process.GetCurrentProcess();

  FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(current.MainModule.FileName);

  Console.WriteLine(myFileVersionInfo.FileVersion);

30.怎样制作一个简单的winform安装程序?

  ① 建一个WinForm应用程序,最最简单的那种。运行。

  ② 添加新项目->安装和部署项目,‘模板’选择‘安装向导’。

  ③ 连续二个‘下一步’,在‘选择包括的项目输出’步骤打勾‘主输出来自’,连续两个‘下一步’,‘完成’。

  ④ 生成。

  ⑤ 到项目目录下找到Setup.exe(还有一个.msi和.ini文件),执行。

31.怎样通过winform安装程序在Sql Server数据库上建表?

  ① [项目]—[添加新项]

  类别:代码;模板:安装程序类。

  名称:MyInstaller.cs

  ② 在SQL Server建立一个表,再[所有任务]—[生成SQL脚本]。

  生成类似如下脚本(注意:把所有GO语句去掉):

  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

  drop table [dbo].[MyTable]

  CREATE TABLE [dbo].[MyTable] (

  [ID] [int] NOT NULL ,

  [NAME] [nchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL

  ) ON [PRIMARY] 

  ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD

  CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED

  (

  [ID]

  ) ON [PRIMARY]

  ③ [项目]—[添加现有项]。mytable.sql—[生成操作]-[嵌入的资源]。

  ④ 将MyInstaller.cs切换到代码视图,添加下列代码:

  先增加:

  using System.Reflection;

  using System.IO;

  然后:

  private string GetSql(string Name)

  {

  try

  {

    Assembly Asm = Assembly.GetExecutingAssembly();

    Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

    StreamReader reader = new StreamReader(strm);

    return reader.ReadToEnd();

  }

  catch (Exception ex)

  {

    Console.Write("In GetSql:"+ex.Message);

    throw ex;

  }

  }

  private void ExecuteSql(string DataBaseName,string Sql)

  {

  System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();

  sqlConn.ConnectionString = "server=myserver; uid=sa; password=; database=master";

  System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);

  Command.Connection.Open();

  Command.Connection.ChangeDatabase(DataBaseName);

  try

  {

  Command.ExecuteNonQuery();

  }

  finally

  {

  Command.Connection.Close();

  }

  }

  protected void AddDBTable(string strDBName)

  {

  try

  {

  ExecuteSql("master","create DATABASE "+ strDBName);

  ExecuteSql(strDBName,GetSql("mytable.sql"));

  }

  catch(Exception ex)

  {

  Console.Write("In exception handler :"+ex.Message);

  }

  }
 public override void Install(System.Collections.IDictionary stateSaver)

  {

  base.Install(stateSaver);

  AddDBTable("MyDB"); //建一个名为MyDB的DataBase

  }

  ⑤ [添加新项目]—[项目类型:安装和部署项目]—[模板:安装项目]—[名称:MySetup]。

  ⑥ [应用程序文件夹]—[添加]—[项目输出]—[主输出]。

  ⑦ 解决方案资源管理器—右键—[安装项目(MySetup)]—[视图]—[自定义操作]。[安装]—[添加自定义操作]—[双击:应用程序文件夹]的[主输出来自***(活动)]。

32.怎样用TreeView显示父子关系的数据库表(winform)?

  三个表a1,a2,a3, a1为a2看母表,a2为a3的母表。

  a1: id, name

  a2: id, parent_id, name

  a3: id, parent_id, name

  用三个DataAdapter把三个表各自Fill进DataSet的三个表。

  用DataRelation设置好三个表之间的关系。  

  foreach(DataRow drA1 in ds.Tables["a1"].Rows)

  {

   tn1 = new TreeNode(drA1["name"].ToString());

   treeView1.Nodes.Add(tn1);

   foreach(DataRow drA2 in drA1.GetChildRows("a1a2"))

   {

  tn2 = new TreeNode(drA2["name"].ToString());

  tn1.Nodes.Add(tn2);

  foreach(DataRow drA3 in drA2.GetChildRows("a2a3"))

  {

   tn3 = new TreeNode(drA3["name"].ToString());

   tn2.Nodes.Add(tn3);

  }

   }

  }

33.怎样从一个form传递数据到另一个form?

  假设Form2的数据要传到Form1的TextBox。

  在Form2:

  // Define delegate

  public delegate void SendData(object sender);

  // Create instance

  public SendData sendData;

  在Form2的按钮单击事件或其它事件代码中:

  if(sendData != null)

  {

   sendData(txtBoxAtForm2);

  }

  this.Close(); //关闭Form2

  在Form1的弹出Form2的代码中:

  Form2 form2 = new Form2();

  form2.sendData = new Form2.SendData(MyFunction);

  form2.ShowDialog();

  ====================

  private void MyFunction(object sender)

  {

  textBox1.Text = ((TextBox)sender).Text;

  }

转载于:https://www.cnblogs.com/kyovcs/archive/2007/07/21/826364.html

C# 开发和使用中的33个技巧相关推荐

  1. C#开发和使用中的23个技巧

    1.怎样定制VC#DataGrid列标题? DataGridTableStyle dgts = new DataGridTableStyle(); dgts.MappingName = "m ...

  2. C# 开发和使用中的32个技巧

    1.怎样定制VC#DataGrid列标题? DataGridTableStyle dgts = new DataGridTableStyle(); dgts.MappingName = "m ...

  3. phpcms v9二次开发及使用中各种问题解决方案(一)

    phpcms v9二次开发及使用中各种问题解决方案(一)模板调用1.内容标签,序号123456789的调用?{pc:contentaction="lists"catid=" ...

  4. Vue 开发必须知道的 36 个技巧【近1W字】

    前言 Vue 3.x 的Pre-Alpha 版本.后面应该还会有 Alpha.Beta 等版本,预计至少要等到 2020 年第一季度才有可能发布 3.0 正式版; 所以应该趁还没出来加紧打好 Vue2 ...

  5. 《安卓开发必知的50个技巧》读书笔记

    记录一下看<安卓开发必知的50个技巧>认为实用的一下技巧. include标签使用注意事项 假设想在标签中覆盖被包括布局所指定的不论什么android:layout_*属性,必须在标签中同 ...

  6. vue Mutation 必须是同步函数 为什么_Vue 开发必须知道的 36 个技巧「近1W字」

    Vue 开发必须知道的 36 个技巧 说明:本文并非原创,来源于segmentfault,搜索文章标题可查看原文 前言 Vue 3.x 的Pre-Alpha 版本.后面应该还会有 Alpha.Beta ...

  7. php gridview,PHP编程:yii2-GridView在开发中常用的功能及技巧总结

    <PHP编程:yii2-GridView在开发中常用的功能及技巧总结>要点: 本文介绍了PHP编程:yii2-GridView在开发中常用的功能及技巧总结,希望对您有用.如果有疑问,可以联 ...

  8. VS开发中的代码编写小技巧——避免重复代码编写的几种方法

    原文:VS开发中的代码编写小技巧--避免重复代码编写的几种方法 上一篇文章中程序员的幸福生活--有你的日子,每天都是情人节,收到了大家的很多好评.鼓励和祝福,非常感动,真诚的谢谢大家.也希望每个朋友都 ...

  9. python开发效率怎样提高_python 提高开发效率的5个小技巧

    很多时候学习是一种难者不会,会者不难的事情. 下面的5个python技巧是性价比极高的知识点,一学就会,不难但是相当管用. 使用交互模式 使用python -i xxxx.py可以直接进入python ...

最新文章

  1. AppStore审核2.1被拒大礼包过审经历
  2. 大学教师辞职创业,已向高校捐赠超10亿元!多以个人名义……
  3. 非常方便的node内置的调试方法
  4. 洛谷 5205 【模板】多项式开根
  5. mkdir,mkdirs区别
  6. php程序设计简明教程
  7. IO静态映射和动态映射
  8. SpringDataJPA入门就这么简单
  9. vb FindwindowEx的用法实例
  10. html JS实现下拉菜单,vue.js怎么实现下拉框
  11. 韦东山linux学习之ubuntu 9.10 软件源 问题
  12. Layout Management
  13. D3D游戏辅助编程开发教程
  14. PySide6将ui文件编译为py文件
  15. gerrit常见错误与处理
  16. 三、Linux文件颜色代表意义
  17. 2021Q2产品团队绩效总结--管理日记
  18. Ubuntu系统shipyard安装
  19. 他一个人干掉了5个装甲军
  20. STM32添加以太网连接

热门文章

  1. highstock 只显示所有 不显示 月_小米34寸曲面显示器深度体验 办公体验极佳 但是还有个大弱点...
  2. mysql事务在提交后才发送给数据库执行_从一个线上问题分析binlog与内部XA事务提交过程...
  3. c语言链表如何做成环,在C语言中单向链表环测试并返回环起始节点的实现方法...
  4. linux下glew例子,一个简单的GLSL Shader例子
  5. c语言 坐标系转换 axistoradius,dynamo编程语言翻译.pdf
  6. java 从mysql 导出到excel_JAVA实现在数据库导出到EXCEL并下载
  7. linux aptana,Ubuntu 下Aptana Studio 3安装使用
  8. cmakelist官方教程_CMakeLists.txt文件如何编写?(一 基础篇)
  9. 支持多种小程序!阿里云ARMS推出小程序监控
  10. 2016年3月9日作业