Welcome

首页 / 软件开发 / C# / C#开发及应用技巧

C#开发及应用技巧2007-09-26 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页";