C # windows form-instrument management system

Source: Internet
Author: User

This experiment compiled a very simple "instrument management system", which is no different from the "library management system" of MFC in the past, and the interface is also very awkward .. You need to know .. This is not my intention .. The general trend, forced to do .. But it is very convenient to get started with C. Books in the library are too old. Borrowing books is not as good as Baidu, and Baidu is not as good as it is .. Fortunately, the property names and function names are all good, and many of them will not be used for a long time. Baidu will not come out, and it will be a success .. That's fun ~~ Wow, haha ~~ It is necessary to summarize it ~ In addition, if someone encounters the same problem as me and has the same information retrieval capability as me, I should be very happy if Baidu comes out of my article ~ Narcissism...

1. The most tangled problem! After the datagridview and SQL Server databases are bound, the primary key "ID" is not expected to be displayed, but the data of a certain row cannot be uniquely identified only according to the content in cells, how can we get the value of "ID" that is not displayed? First, I found a databounditem, which is quite similar. After tostring, The datarowview is displayed, and it is an object, so I want to turn it around, it's okay to use it like dataset ~! PS: I took it for granted that the dataset result failed ..

The program roughly looks like this: Click OK to get the ID of this row of data. This event is from the datagridview.


The code after the event is triggered is as follows:

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {              DataGridView dgv = (DataGridView)sender;              if (dgv.Columns[e.ColumnIndex].Name == "ok")              {                  String now = DateTime.Now.ToString("yyyy-MM-dd");                  DataRowView drv = (DataRowView)dgv.Rows[e.RowIndex].DataBoundItem;                  String id = drv.Row["id"].ToString();              }

2. To sum up the question about date in the database, there are some small aspects.

How to get the date to the database from datetimepicker:

Set the "format" attribute of the space to custom, and add the control name to the database. customformat = "yyyy-mm-dd ". Then, you can directly add it using the control name. Text.

There are many good methods to obtain the current date: datetime now = datetime. Now. For example, you can use adddays () or other functions to add or subtract a date. The parameter can also be a negative value, which is very convenient. Then, call its tostring ("yyyy-mm-dd") method to compare the data of the "date" type in the database, and use "<">.

3. After the data is bound, there is almost no need to write code for searching. In the dataset Editor, add functions, such as select, update, and delete statements, and directly write SQL statements and then call them. Even if the content in the database table is modified, you do not need to rereference it. Add the new columns to the SELECT statement of the corresponding fill and other functions, it does not matter if you do not select a new column name when editing the column, just enter it manually.

4. There is another quite tangled problem. After the data is bound, for example, a table is bound to the datagridview. Adding unbound columns cannot add content by yourself. You can select "OK", "modify", and other buttons. Each displayed text is the same. In this case, you can add an event to a row of buttons by adding event processing to the datagridview. The Code is as follows:

Private void maid (Object sender, maid e) {datagridview dgv = (datagridview dview) sender; datarowview DRV = (datarowview) dgv. rows [E. rowindex]. databounditem; int32 id = convert. toint32 (DRV. row ["ID"]. tostring (); If (dgv. columns [E. columnindex]. name = "OK") {string now = datetime. now. tostring ("yyyy-mm-dd"); // todo: This line of code loads data to the table "apparatusmanagerdataset5.borrow" . You can move or delete it as needed. This. borrowtableadapter. OK (now, ID); MessageBox. Show ("OK! The lending date is today ");} else if (dgv. columns [E. columnindex]. name = "delete") {This. borrowtableadapter. deletequery (ID); MessageBox. show ("this record has been deleted! ");}}


However, you cannot add an unbound column to display the content as needed. However, data is usually read from the database. You can add a View to the database. Select a view for the data source to associate the two tables. For example, my previous data source adds a table with a lending record, but the lending record contains the item ID, and there is no name for storing the item. If you want to read data from an item table, you are planning to operate it in the code, but you cannot assign values to unbound columns. Just add the view. The bound option directly contains the content of the column selected from the item table. The operation is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.