C# 开发和使用中的23个精华技巧
|
源代码网整理以下1.怎样定制VC#DataGrid列标题? DataGridTableStyle dgts = new DataGridTableStyle(); 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启动时直接最大化? ⑥ 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; 软件开发网 www.mscto.com } 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绑定两个主从关系的表? 源代码网推荐 源代码网供稿. |
