Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills in the development and use of C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the skills in the development and use of C#. What is introduced in this article is very detailed and has a certain reference value. Interested friends must read it!

1. How to customize VC#DataGrid column headings?

DataGridTableStyle dgts = new DataGridTableStyle (); dgts.MappingName = "myTable"; / / myTable is the DataTable DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn () to load the data; dgcs.MappingName = "title_id"; dgcs.HeaderText = "title ID"; dgts.GridColumnStyles.Add (dgcs); …. DataGrid1.TableStyles.Add (dgts)

two。 How to write a conditional statement to retrieve all records with an empty field?

... where col_name is null

3. How to receive enter key input in c # Winform application?

Set the AcceptButton of form.

4. For example, what should NUMBER (15) in Oracle be in Sql Server?

NUMBER (15): try it with numeric with a precision of 15.

How to write the stored procedure that applies the like statement to 5.sql server?

Select * from mytable where haoma like'%'+ @ hao +'%'

How do I get textBox to accept the enter message in 6.vc# winform (if there is no button)?

Private void textBox1_KeyPress (object sender, System.Windows.Forms.KeyPressEventArgs e) {if (e.KeyChar! = (char) 13) return; else / / do something;}

7. Why does (Int32) cmd.ExecuteScalar () prompt that the conversion is invalid when assigning to the Int32 variable?

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

How to add a column to the DataGrid in which 8.DataSource is a child table to display a field in the parent table?

Manually add a column to the child table.

DataColumn dc = new DataColumn ("newCol", Type.GetType ("System.String")); dc.Expression = "Parent.parentColumnName"; dt.Columns.Add (dc); / / dt is the child table

9. How do I make DataGrid display only a portion of the data for a column in DataTable?

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

10. How to make the combobox of winform can only be selected but not entered?

The DropDownStyle property determines whether the user can enter new values in the text section and whether the list section is always displayed.

Value:

DropDown-the text is editable. The user must click the arrow button to display the list section.

DropDownList-users cannot edit the text directly. The user must click the arrow button to display the list section.

11. How to make the date displayed in the DataGrid of winform show only part of the year, month and day, removing the time?

Add to_date (date field, 'yyyy-mm-dd') to the sql statement

twelve。 How to merge two columns of a database table into one column Fill into DataSet?

DcChehao = new DataColumn ("newColumnName", typeof (string))

DcChehao.Expression = "columnName1+columnName2"

Dt.Columns.Add (dcChehao)

Oracle:

Select col1col2 from table

Sql server:

Select col1+col2 from table

13. How to extract the text in parentheses from the merged field as the display of DataGrid or other bound controls? That is, the text between the left parentheses (and closing parentheses) in the merged field is extracted.

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. When using the mouse wheel to browse DataGrid data beyond a certain range, DataGrid will lose focus. How to solve it?

This.dataGrid1.MouseWheel+=new MouseEventHandler (dataGrid1_MouseWheel); private void dataGrid1_MouseWheel (object sender, MouseEventArgs e) {this.dataGrid1.Select ();}

15. How to change the'+ 'symbol of keyboard input into' A'?

In the KeyPress event of textBox

If (e.KeyChar = ='+') {SendKeys.Send ("A"); e.Handled = true;}

16. How to make Winform start up directly?

This.WindowState = FormWindowState.Maximized

How does 17.c# get the current date and time, and what is it in the sql statement?

C#: DateTime.Now

Sql server: GetDate ()

18. How do I access a row or column of winform DataGrid, or every row and column?

DataGrid [row,col]

19. How to summarize for DataTable, for example, how much is the column value of 'Yanji' of DataTable?

Dt.Select ("City = Yanji"). Length

When 20.DataGrid data is exported to Excel, 0212 and so on will become 212. How to make it continue to display as 0212 after export?

Range.NumberFormat = "0000"

21. How does ① export DataGrid data to Excel for printing?

② has previously set up TableStyle for DataGrid, that is, you have customized column headings and columns to display. What if you want to export data in a custom view?

After ③ exports the data to Excel, how to set the border for it?

How does ④ align a column exported from DataGrid to Excel?

After ⑤ data is exported from DataGrid to Excel, how do I make the title line appear on each page when printing?

After ⑥ DataGrid data is exported to Excel, each page displays' current page / total number of pages' when printing. How can I achieve this?

one

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 [_ 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.HeaderText; / / index can be traversed from 0~dataGrid1.TableStyles [0] .GridColumnStyles.Count.

③ Excel.Range range

Range=worksheet.get_Range (worksheet.Cells [1pr 1], xSt.Cells [ds.Tables [0] .Rows.Count + 1Magneds.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 = "$1rime 1"

⑥ worksheet.PageSetup.CenterFooter = "Page & P / Total & N"

twenty-two。 When assigning the Cell content of DataGrid to Excel, you want to show the progress on the CaptionText of DataGrid, but not. WHY?

...

DataGrid1.CaptionText = "exporting:" + (row + 1) + "/" + row_cnt

System.Windows.Forms.Application.DoEvents ()

...

Processes all Windows messages currently in the message queue.

When you run a Windows form, it creates a new form, and then the form waits for the event to be handled. Each time the form handles an event, it processes all code associated with the event. All other events are waiting in the queue. The application does not respond when the code handles the event. If you call DoEvents in your code, the application can handle other events.

If you remove DoEvents from your code, the form will not be redrawn until the execution of the button's stand-alone event handler ends. This method is usually used in loops to process messages.

23. How do I call an external program, such as a C # compiled .exe, from Flash?

Fscommand ("exec", "application .exe")

① must publish flash as .exe

② must create a subdirectory called fscommand in the directory where the .exe file generated by flash is located, and copy the executable program to be called there.

24. Is there any way to use code to control the up and down, left and right scrolling of DataGrid?

DataGrid1.Select (); SendKeys.Send ("{PGUP}"); SendKeys.Send ("{PGDN}"); SendKeys.Send ("{^ {LEFT}"); / / Ctrl+ left arrow SendKeys.Send ("{^ {RIGHT}"); / / Ctrl+ right arrow

25. How do I bind two DataGrid to two master-slave tables?

DataGrid1.DataSource = ds; DataGrid1.DataMember = "parent";. DataGrid2.DataSouce = ds; DataGrid2.DataMember = "parent table. relationship name"

How can the version number of 26.assembly be generated automatically? Especially when the program is not written in the VStudio environment under Console.

The key is [assembly: AssemblyVersion ("1.0.*")] in AssemblyInfo.cs. The command line contains AssemblyInfo.cs when compiled.

twenty-seven。 How to build a Shared Assembly?

Generate a Strong Name:keyfile.sn with sn.exe and put it in the source program directory

In the AssemblyInfo.cs of the project [assembly: AssemblyKeyFile ("..\..\\ keyfile.sn")]

After generating dll, use gacutil / I myDll.dll to put it into Global Assembly Cach.

twenty-eight。 How do I get a record between the capital letters of a certain field in Oracle?

Select * from table where ascii (substr (field, 1mem1)) between ascii ('A') and ascii ('Z')

twenty-nine。 How to get the version number of the current Assembly?

Process current = Process.GetCurrentProcess (); FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo (current.MainModule.FileName); Console.WriteLine (myFileVersionInfo.FileVersion)

thirty。 How to make a simple winform installer?

① builds a WinForm application, the simplest kind. Run.

② add New Project-> install and deploy the project, select 'template' and select 'installation Wizard'.

③ has two consecutive "next steps", ticking "main output from" in the "Select included project output" step, and two consecutive "next steps" and "done".

④ generation.

⑤ goes to the project directory to find Setup.exe (and a .msi and .ini file), and execute.

thirty-one。 How to create tables on the Sql Server database through the winform installer?

① [Project]-[add New item]

Category: code; template: installer class.

Name: MyInstaller.cs

② creates a table in SQL Server, and then [all tasks]-[generate SQL script].

Generate a script similar to the following (note: remove all GO statements):

If exists (select * from dbo.sysobjects where id = object_id (N' [dbo]. [MyTable]') and OBJECTPROPERTY (id, NumberIsUserTable`) = 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]

③ [Project]-[add existing item]. Mytable.sql- [generate operation]-[embedded resources].

④ switches MyInstaller.cs to code view and adds the following code:

First add:

Using System.Reflection; using System.IO

Then:

Private string GetSql (string Name) {try {Assembly Asm = Assembly.GetExecutingAssembly (); Stream strm = Asm.GetManifestResourceStream (Asm.GetN ame (). 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"); / / build a DataBase named MyDB}

⑤ [add New Project]-[Project Type: installation and deployment Project]-[template: installation Project]-[name: MySetup].

⑥ [Application folder]-[add]-[Project output]-[main output].

⑦ solution Explorer-right-click-[installation Project (MySetup)]-[View]-[Custom Action]. [install]-[add Custom Action]-[double click: application folder] [main output from * (active)].

thirty-two。 How to use TreeView to display the database table of parent-child relationship (winform)?

Three tables: a _ 1, a _ 2, a _ 2, a _ 3, a _ 1, a _ 2, a _ 2, a _ 3.

A1: id, name

A2: id, parent_id, name

A3: id, parent_id, name

Use three DataAdapter to Fill each of the three tables into the three tables of DataSet.

Set the relationship between the three tables with 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);}

thirty-three。 How do I transfer data from one form to another form?

Suppose Form2's data is to be transferred to Form1's TextBox.

In Form2:

/ / Define delegate public delegate void SendData (object sender); / / Create instance public SendData sendData

In the button click event or other event code in Form2:

If (sendData! = null) {sendData (txtBoxAtForm2);} this.Close (); / / close Form2

In the code that pops up Form2 in Form1:

Form2 form2 = new Form2 (); form2.sendData = new Form2.SendData (MyFunction); form2.ShowDialog (); private void MyFunction (object sender) {textBox1.Text = ((TextBox) sender) .Text;} these are all the contents of the article "what are the skills in the development and use of C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report