zoukankan      html  css  js  c++  java
  • 23个C#实用技巧

    1.  这些技巧不好找,我归类总结了一下,对大家访问使用也方便,好了,列表如下:  
    2.   1.怎样定制VC#DataGrid列标题?   
    3.   
    4.   DataGridTableStyle dgts = new DataGridTableStyle();   
    5.   
    6.   dgts.MappingName = "myTable"//myTable为要载入数据的DataTable    
    7.   
    8.      
    9.   
    10.   DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();   
    11.   
    12.   dgcs.MappingName = "title_id";   
    13.   
    14.   dgcs.HeaderText = "标题ID";   
    15.   
    16.   dgts.GridColumnStyles.Add(dgcs);   
    17.   
    18.   。。。   
    19.   
    20.   dataGrid1.TableStyles.Add(dgts);   
    21.   
    22.   2.检索某个字段为空的所有记录的条件语句怎么写?   
    23.   
    24.   ...where col_name is null   
    25.   
    26.   3.如何在c# Winform应用中接收回车键输入?   
    27.   
    28.   设一下form的AcceptButton.   
    29.   
    30.   4.比如Oracle中的NUMBER(15),在Sql Server中应是什么?   
    31.   
    32.   NUMBER(15):用numeric,精度15试试。   
    33.   
    34.   5.sql server的应用like语句的存储过程怎样写?   
    35.   
    36.   select * from mytable where haoma like ‘%’ + @hao + ‘%’   
    37.   
    38.   6.vc# winform中如何让textBox接受回车键消息(假没没有按钮的情况下)?   
    39.   
    40.   private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)   
    41.   
    42.   {   
    43.   
    44.   if(e.KeyChar != (char)13)   
    45.   
    46.   return;   
    47.   
    48.   else   
    49.   
    50.   //do something;    
    51.   
    52.   }   
    53.   
    54.   7.为什么(Int32)cmd.ExecuteScalar()赋值给Int32变量时提示转换无效?   
    55.   
    56.   Int32.Parse(cmd.ExecuteScalar().ToString());   
    57.   
    58.   8.DataSource为子表的DataGrid里怎样增加一个列以显示母表中的某个字段?   
    59.   
    60.   在子表里手动添加一个列。   
    61.   
    62.   DataColumn dc = new DataColumn("newCol", Type.GetType("System.String"));   
    63.   
    64.   dc.Expression = "Parent.parentColumnName";   
    65.   
    66.   dt.Columns.Add(dc); //dt为子表    
    67.   
    68.   9.怎样使DataGrid显示DataTable中某列的数据时只显示某一部分?   
    69.   
    70.   select ..., SUBSTR(string, start_index, end_index) as ***, *** from ***   
    71.   
    72.   10.如何让winform的combobox只能选不能输入?   
    73.   
    74.   DropDownStyle 属性确定用户能否在文本部分中输入新值以及列表部分是否总显示。   
    75.   
    76.   值:   
    77.   
    78.   DropDown --- 文本部分可编辑。用户必须单击箭头按钮来显示列表部分。   
    79.   
    80.   DropDownList --- 用户不能直接编辑文本部分。用户必须单击箭头按钮来显示列表部分。   
    81.   
    82.   Simple --- 文本部分可编辑。列表部分总可见。   
    83.   
    84.   11.怎样使winform的DataGrid里显示的日期只显示年月日部分,去掉时间?   
    85.   
    86.   sql语句里加上to_date(日期字段,'yyyy-mm-dd')   
    87.   
    88.   12.怎样把数据库表的二个列合并成一个列Fill进DataSet里?   
    89.   
    90.   dcChehao = new DataColumn("newColumnName"typeof(string));   
    91.   
    92.   dcChehao.Expression = "columnName1+columnName2";   
    93.   
    94.   dt.Columns.Add(dcChehao);   
    95.   
    96.   Oracle:   
    97.   
    98.   select col1  col2 from table   
    99.   
    100.   sql server:   
    101.   
    102.   select col1+col2 from table   
    103.   
    104.   13.如何从合并后的字段里提取出括号内的文字作为DataGrid或其它绑定控件的显示内容?即把合并后的字段内容里的左括号(和右括号)之间的文字提取出来。   
    105.   
    106.   Select COL1,COL2, case   
    107.   
    108.   when COL3 like ‘%(%’ THEN substr(COL3, INSTR(COL3, ‘(’ )+1, INSTR(COL3,‘)’)-INSTR(COL3,‘(’)-1)   
    109.   
    110.   end as COL3   
    111.   
    112.   from MY_TABLE   
    113.   
    114.   14.当用鼠标滚轮浏览DataGrid数据超过一定范围DataGrid会失去焦点。怎样解决?   
    115.   
    116.   this.dataGrid1.MouseWheel+=new MouseEventHandler(dataGrid1_MouseWheel);   
    117.   
    118.   private void dataGrid1_MouseWheel(object sender, MouseEventArgs e)   
    119.   
    120.   {   
    121.   
    122.   this.dataGrid1.Select();   
    123.   
    124.   }   
    125.   
    126.   15.怎样把键盘输入的‘+’符号变成‘A’?   
    127.   
    128.   textBox的KeyPress事件中   
    129.   
    130.   if(e.KeyChar == '+')   
    131.   
    132.   {   
    133.   
    134.   SendKeys.Send("A");   
    135.   
    136.   e.Handled = true;   
    137.   
    138.   }   
    139.   
    140.   16.怎样使Winform启动时直接最大化?   
    141.   
    142.   this.WindowState = FormWindowState.Maximized;   
    143.   
    144.   17.c#怎样获取当前日期及时间,在sql语句里又是什么?   
    145.   
    146.   c#: DateTime.Now   
    147.   
    148.   sql server: GetDate()   
    149.   
    150.   18.怎样访问winform DataGrid的某一行某一列,或每一行每一列?   
    151.   
    152.   dataGrid[row,col]   
    153.   
    154.   19.怎样为DataTable进行汇总,比如DataTable的某列值‘延吉'的列为多少?   
    155.   
    156.   dt.Select("城市='延吉'").Length;   
    157.   
    158.   20.DataGrid数据导出到Excel后0212等会变成212。怎样使它导出后继续显示为0212?   
    159.   
    160.   range.NumberFormat = "0000";  
    161. 21.   
    162.   
    163.   ① 怎样把DataGrid的数据导出到Excel以供打印?   
    164.   
    165.   ② 之前已经为DataGrid设置了TableStyle,即自定义了列标题和要显示的列,如果想以自定义的视图导出数据该怎么办?   
    166.   
    167.   ③ 把数据导出到Excel后,怎样为它设置边框啊?   
    168.   
    169.   ④ 怎样使从DataGrid导出到Excel的某个列居中对齐?   
    170.   
    171.   ⑤ 数据从DataGrid导出到Excel后,怎样使标题行在打印时出现在每一页?   
    172.   
    173.   ⑥ DataGrid数据导出到Excel后打印时每一页显示’当前页/共几页’,怎样实现?   
    174.   
    175.   ①   
    176.   
    177.   private void button1_Click(object sender, System.EventArgs e)   
    178.   
    179.   {   
    180.   
    181.   int row_index, col_index;   
    182.   
    183.      
    184.   
    185.   row_index = 1;   
    186.   
    187.   col_index = 1;   
    188.   
    189.      
    190.   
    191.   Excel.ApplicationClass excel = new Excel.ApplicationClass();   
    192.   
    193.   excel.Workbooks.Add(true);   
    194.   
    195.      
    196.   
    197.   DataTable dt = ds.Tables["table"];   
    198.   
    199.      
    200.   
    201.   foreach(DataColumn dcHeader in dt.Columns)   
    202.   
    203.   excel.Cells[row_index, col_index++] = dcHeader.ColumnName;   
    204.   
    205.      
    206.   
    207.   foreach(DataRow dr in dt.Rows)   
    208.   
    209.   {   
    210.   
    211.   col_index = 0;   
    212.   
    213.   foreach(DataColumn dc in dt.Columns)   
    214.   
    215.   {   
    216.   
    217.   excel.Cells[row_index+1, col_index+1] = dr[dc];   
    218.   
    219.   col_index++;   
    220.   
    221.   }   
    222.   
    223.   row_index++;   
    224.   
    225.   }   
    226.   
    227.   excel.Visible = true;   
    228.   
    229.      
    230.   
    231.   }   
    232.   
    233.      
    234.   
    235.   private void Form1_Load(object sender, System.EventArgs e)   
    236.   
    237.   {   
    238.   
    239.   SqlConnection conn = new SqlConnection("server=tao; uid=sa; pwd=; database=pubs");   
    240.   
    241.   conn.Open();   
    242.   
    243.      
    244.   
    245.   SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);   
    246.   
    247.   ds = new DataSet();   
    248.   
    249.   da.Fill(ds, "table");   
    250.   
    251.      
    252.   
    253.   dataGrid1.DataSource = ds;   
    254.   
    255.   dataGrid1.DataMember = "table";   
    256.   
    257.   }   
    258.   
    259.   ②dataGrid1.TableStyles[0].GridColumnStyles[index].HeaderText; //index可以从0~dataGrid1.TableStyles[0].GridColumnStyles.Count遍历。    
    260.   
    261.   ③ Excel.Range range;   
    262.   
    263.   range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Count]);   
    264.   
    265.      
    266.   
    267.   range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);   
    268.   
    269.      
    270.   
    271.   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;   
    272.   
    273.   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;   
    274.   
    275.   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;   
    276.   
    277.      
    278.   
    279.   range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;   
    280.   
    281.   range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;   
    282.   
    283.   range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;   
    284.   
    285.   ④ range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter   
    286.   
    287.   ⑤ worksheet.PageSetup.PrintTitleRows = "$1:$1";   
    288.   
    289.   ⑥ worksheet.PageSetup.CenterFooter = "第&P页 / 共&N页";   
    290.   
    291.   22.当把DataGrid的Cell内容赋值到Excel的过程中想在DataGrid的CaptionText上显示进度,但不显示。WHY?   
    292.   
    293.   ...   
    294.   
    295.   dataGrid1.CaptionText = "正在导出:" + (row + 1) + "/" + row_cnt;   
    296.   
    297.   System.Windows.Forms.Application.DoEvents();   
    298.   
    299.   ...   
    300.   
    301.      
    302.   
    303.   处理当前在消息队列中的所有Windows消息。   
    304.   
    305.      
    306.   
    307.   当运行Windows窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。在代码处理事件时,应用程序并不响应。如果在代码中调用DoEvents,则应用程序可以处理其他事件。   
    308.   
    309.   如果从代码中移除DoEvents,那么在按钮的单机事件处理程序执行结束以前,窗体不会重新绘制。通常在循环中使用该方法来处理消息。  
    310.   23.怎样从Flash调用外部程序,如一个C#编译后生成的.exe?   
    311.   
    312.   fscommand("exec""应用程序.exe");   
    313.   
    314.   ① 必须把flash发布为.exe   
    315.   
    316.   ② 必须在flash生成的.exe文件所在目录建一个名为fscommand的子目录,并把要调用的可执行程序拷贝到那里。   
    317.   
    318.   24.有没有办法用代码控制DataGrid的上下、左右的滚动?   
    319.   
    320.   dataGrid1.Select();   
    321.   
    322.   SendKeys.Send("{PGUP}");   
    323.   
    324.   SendKeys.Send("{PGDN}");   
    325.   
    326.   SendKeys.Send("{^{LEFT}"); // Ctrl+左方向键    
    327.   
    328.   SendKeys.Send("{^{RIGHT}"); // Ctrl+右方向键    
    329.   
    330.   25.怎样使两个DataGrid绑定两个主从关系的表?   
    331.   
    332.   DataGrid1.DataSource = ds;   
    333.   
    334.   DataGrid1.DataMember = "母表";   
    335.   
    336.   ...   
    337.   
    338.   DataGrid2.DataSouce = ds;   
    339.   
    340.   DataGrid2.DataMember = "母表.关系名";   
    341.   
    342.   26.assembly的版本号怎样才能自动生成?特别是在Console下没有通过VStudio环境编写程序时。   
    343.   
    344.   关键是AssemblyInfo.cs里的[assembly: AssemblyVersion("1.0.*")],命令行编译时包含AssemblyInfo.cs   
    345.   
    346.   27.怎样建立一个Shared Assembly?   
    347.   
    348.   用sn.exe生成一个Strong Name:keyfile.sn,放在源程序目录下   
    349.   
    350.   在项目的AssemblyInfo.cs里[assembly: AssemblyKeyFile("..\\..\\keyfile.sn")]   
    351.   
    352.   生成dll后,用gacutil /i myDll.dll放进Global Assembly Cach.   
    353.   
    354.   28.在Oracle里如何取得某字段第一个字母为大写英文A~Z之间的记录?   
    355.   
    356.   select * from table where ascii(substr(字段,1,1)) between ascii('A') and ascii('Z')   
    357.   
    358.   29.怎样取得当前Assembly的版本号?   
    359.   
    360.   Process current = Process.GetCurrentProcess();   
    361.   
    362.   FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(current.MainModule.FileName);   
    363.   
    364.   Console.WriteLine(myFileVersionInfo.FileVersion);   
    365.   
    366.   30.怎样制作一个简单的winform安装程序?   
    367.   
    368.   ① 建一个WinForm应用程序,最最简单的那种。运行。   
    369.   
    370.   ② 添加新项目->安装和部署项目,‘模板’选择‘安装向导’。   
    371.   
    372.   ③ 连续二个‘下一步’,在‘选择包括的项目输出’步骤打勾‘主输出来自’,连续两个‘下一步’,‘完成’。   
    373.   
    374.   ④ 生成。   
    375.   
    376.   ⑤ 到项目目录下找到Setup.exe(还有一个.msi和.ini文件),执行。   
    377.   
    378.   31.怎样通过winform安装程序在Sql Server数据库上建表?   
    379.   
    380.   ① [项目]—[添加新项]   
    381.   
    382.   类别:代码;模板:安装程序类。   
    383.   
    384.   名称:MyInstaller.cs   
    385.   
    386.   ② 在SQL Server建立一个表,再[所有任务]—[生成SQL脚本]。   
    387.   
    388.   生成类似如下脚本(注意:把所有GO语句去掉):   
    389.   
    390.   if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)   
    391.   
    392.   drop table [dbo].[MyTable]   
    393.   
    394.      
    395.   
    396.   CREATE TABLE [dbo].[MyTable] (   
    397.   
    398.   [ID] [int] NOT NULL ,   
    399.   
    400.   [NAME] [nchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL   
    401.   
    402.   ) ON [PRIMARY]   
    403.   
    404.      
    405.   
    406.      
    407.   
    408.   ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD   
    409.   
    410.   CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED   
    411.   
    412.   (   
    413.   
    414.   [ID]   
    415.   
    416.   ) ON [PRIMARY]   
    417.   
    418.   ③ [项目]—[添加现有项]。mytable.sql—[生成操作]-[嵌入的资源]。   
    419.   
    420.   ④ 将MyInstaller.cs切换到代码视图,添加下列代码:   
    421.   
    422.   先增加:   
    423.   
    424.   using System.Reflection;   
    425.   
    426.   using System.IO;   
    427.   
    428.   然后:   
    429.   
    430.   private string GetSql(string Name)   
    431.   
    432.   {   
    433.   
    434.   try   
    435.   
    436.   {   
    437.   
    438.     Assembly Asm = Assembly.GetExecutingAssembly();   
    439.   
    440.     Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);   
    441.   
    442.     StreamReader reader = new StreamReader(strm);   
    443.   
    444.     return reader.ReadToEnd();   
    445.   
    446.   }   
    447.   
    448.   catch (Exception ex)   
    449.   
    450.   {   
    451.   
    452.     Console.Write("In GetSql:"+ex.Message);   
    453.   
    454.     throw ex;   
    455.   
    456.   }   
    457.   
    458.   }   
    459.   
    460.      
    461.   
    462.   private void ExecuteSql(string DataBaseName,string Sql)   
    463.   
    464.   {   
    465.   
    466.   System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();   
    467.   
    468.   sqlConn.ConnectionString = "server=myserver; uid=sa; password=; database=master";   
    469.   
    470.   System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);   
    471.   
    472.      
    473.   
    474.   Command.Connection.Open();   
    475.   
    476.   Command.Connection.ChangeDatabase(DataBaseName);   
    477.   
    478.   try   
    479.   
    480.   {   
    481.   
    482.   Command.ExecuteNonQuery();   
    483.   
    484.   }   
    485.   
    486.   finally   
    487.   
    488.   {   
    489.   
    490.   Command.Connection.Close();   
    491.   
    492.   }   
    493.   
    494.   }   
    495.   
    496.   protected void AddDBTable(string strDBName)   
    497.   
    498.   {   
    499.   
    500.   try   
    501.   
    502.   {   
    503.   
    504.   ExecuteSql("master","create DATABASE "+ strDBName);   
    505.   
    506.   ExecuteSql(strDBName,GetSql("mytable.sql"));   
    507.   
    508.   }   
    509.   
    510.   catch(Exception ex)   
    511.   
    512.   {   
    513.   
    514.   Console.Write("In exception handler :"+ex.Message);   
    515.   
    516.   }   
    517.   
    518.   }   
    519.   
    520.      
    521.   
    522.   public override void Install(System.Collections.IDictionary stateSaver)   
    523.   
    524.   {   
    525.   
    526.   base.Install(stateSaver);   
    527.   
    528.   AddDBTable("MyDB"); //建一个名为MyDB的DataBase    
    529.   
    530.   }   
    531.   
    532.   ⑤ [添加新项目]—[项目类型:安装和部署项目]—[模板:安装项目]—[名称:MySetup]。   
    533.   
    534.   ⑥ [应用程序文件夹]—[添加]—[项目输出]—[主输出]。   
    535.   
    536.   ⑦ 解决方案资源管理器—右键—[安装项目(MySetup)]—[视图]—[自定义操作]。[安装]—[添加自定义操作]—[双击:应用程序文件夹]的[主输出来自***(活动)]。   
    537.   
    538.   32.怎样用TreeView显示父子关系的数据库表(winform)?   
    539.   
    540.   三个表a1,a2,a3, a1为a2看母表,a2为a3的母表。   
    541.   
    542.   a1: id, name   
    543.   
    544.   a2: id, parent_id, name   
    545.   
    546.   a3: id, parent_id, name   
    547.   
    548.   用三个DataAdapter把三个表各自Fill进DataSet的三个表。   
    549.   
    550.   用DataRelation设置好三个表之间的关系。   
    551.   
    552.      
    553.   
    554.   foreach(DataRow drA1 in ds.Tables["a1"].Rows)   
    555.   
    556.   {   
    557.   
    558.    tn1 = new TreeNode(drA1["name"].ToString());   
    559.   
    560.    treeView1.Nodes.Add(tn1);   
    561.   
    562.    foreach(DataRow drA2 in drA1.GetChildRows("a1a2"))   
    563.   
    564.    {   
    565.   
    566.   tn2 = new TreeNode(drA2["name"].ToString());   
    567.   
    568.   tn1.Nodes.Add(tn2);   
    569.   
    570.   foreach(DataRow drA3 in drA2.GetChildRows("a2a3"))   
    571.   
    572.   {   
    573.   
    574.    tn3 = new TreeNode(drA3["name"].ToString());   
    575.   
    576.    tn2.Nodes.Add(tn3);   
    577.   
    578.   }   
    579.   
    580.    }   
    581.   
    582.   }   
    583.   
    584.   33.怎样从一个form传递数据到另一个form?   
    585.   
    586.   假设Form2的数据要传到Form1的TextBox。   
    587.   
    588.   在Form2:   
    589.   
    590.   // Define delegate    
    591.   
    592.   public delegate void SendData(object sender);   
    593.   
    594.   // Create instance    
    595.   
    596.   public SendData sendData;   
    597.   
    598.   在Form2的按钮单击事件或其它事件代码中:   
    599.   
    600.   if(sendData != null)   
    601.   
    602.   {   
    603.   
    604.    sendData(txtBoxAtForm2);   
    605.   
    606.   }   
    607.   
    608.   this.Close(); //关闭Form2    
    609.   
    610.   在Form1的弹出Form2的代码中:   
    611.   
    612.   Form2 form2 = new Form2();   
    613.   
    614.   form2.sendData = new Form2.SendData(MyFunction);   
    615.   
    616.   form2.ShowDialog();   
    617.   
    618.   ====================   
    619.   
    620.   private void MyFunction(object sender)   
    621.   
    622.   {   
    623.   
    624.   textBox1.Text = ((TextBox)sender).Text;   
    625.   
    626.   

    摘自:http://www.soaspx.com/dotnet/csharp/csharp_20111116_8297.html

  • 相关阅读:
    CEF解决加载慢问题
    CEF之CefSettings设置locale
    Win32程序支持命令行参数的做法(转载)
    VC++实现程序重启的方法(转载)
    CEF之CefSettings设置日志等级
    libcurl开源库在Win32程序中使用下载文件显示进度条实例
    libcurl开源库在Win7 + VS2012环境下编译、配置详解 以及下载文件并显示下载进度 demo(转载)
    使用ShellExecute打开目标文件所在文件夹并选中目标文件
    linux下gimp的使用
    linux下的chm阅读器?
  • 原文地址:https://www.cnblogs.com/graypigeon/p/2373194.html
Copyright © 2011-2022 走看看