Do management software always in and datatable,dataset,datagridview dealing with, used often, but their own thinking of the problem is not much, with the most stupid method, made out are the most ugly interface and inefficient procedures, Last week one night to DataGridView serious research, mainly is the interface of things, feel that I improved after the interface looks is not the same, oh ~ I think it looks also possible interface code:
Copy Code code as follows:
DataGridViewCellStyle style = new DataGridViewCellStyle (); Customize a cell style.
Style. BackColor = Color.lightcyan; It's actually a different color.
Datagridview1.rowheadersvisible = false; Think that the box that shows the line head is not good-looking, remove ~
Datagridview1.selectionmode = Datagridviewselectionmode.fullrowselect; It's convenient to see the data by clicking on a cell and selecting its row.
Datagridview1.columnheadersheightsizemode = datagridviewcolumnheadersheightsizemode.disableresizing; The column title bar size cannot be resized by the user.
Datagridview1.autosizecolumnsmode = Datagridviewautosizecolumnsmode.allcells; Column width style, the longest data can all be displayed so that "..." will not appear.
Datagridview1.alternatingrowsdefaultcellstyle = style; The odd-numbered line style is set to the previously defined style, giving the adjacent rows a color contrast.
Still have a lot of effect useless ~ But such simple few lines still can quite big improve the beauty of the procedure ~
And then talk about the new understanding of data binding, always know this thing, but do not know how to use, also did not take time to study, but these days in the CSDN forum, as if a lot of people use this aspect of the problem, so I have the motivation to study these usages. Hey ~ sometimes feel lazy ~ often to do data processing aspects of things , even today is the first time to have a deep understanding of it ~ Shame!!
Let's talk about the implementation process. ~ The SqlConnection and SqlCommand of the SQL Server database are necessary ~ followed by SqlDataAdapter (in fact this is often used, but only use it to read data), in fact, this data adapter is very useful, Using it can be very convenient to read data from the database, through DataGridView display to the user, the user changes the data in DataGridView, and then through the SqlDataAdapter can be modified to reflect the data in the database, The period does not require us to write any operational database code. NET Framework for us to do! And this data is stored in a dataset or a DataTable such a cache, do not have to keep the database connection ~ Oh forget, also need to use SqlCommandBuilder to generate some modified SQL statements, The simple implementation code is as follows:
Copy Code code as follows:
DataSet ds = null;
SqlDataAdapter adapter = null;
SqlCommandBuilder sqlbuilder = null;
This button reads the data and binds the data
private void Button1_Click (object sender, EventArgs e)
... {
ds = new DataSet ();
SqlConnection cn = new SqlConnection ("Data source=.; User Id=sa; Password=0;initial Catalog=bmis ");
SqlCommand cm = new SqlCommand ();
adapter = new SqlDataAdapter (cm);
Sqlbuilder = new SqlCommandBuilder (adapter);
Cm. Connection = CN;
Cm.commandtype = CommandType.Text;
Cm.commandtext = "SELECT * FROM [Providerinfo]";
Adapter. Fill (DS); This method puts the resulting data into a DataTable.
Datagridview1.datasource = ds. Tables[0]; Bind data to DataGridView
}
Returns the modified data to the database.
private void Button2_Click (object sender, EventArgs e)
... {
Adapter. Update (DS);
}
The data changes that users make in the DataGridView are reflected in the DataTable they bind to, and the SqlDataAdapter Update method is used to reflect the changes to the database, and the sense of using the DataTable is an intermediate storage data tool, SqlDataAdapter is an intermediary data manipulation tool that combines the tasks of updating a data source.
now I want to talk about what this code does in sql server after it's run. First, suppose that after I display the data in DataGridView, A row of data was modified, and one row of data was added. Open the Sql server event Explorer, the whole process of monitoring. Log in, execute the query statement is not the same, but when I execute an update, I will call a named sp_reset_ Connection stored procedures, see the name guess should be connected to the database meaning, and then for each row modified data to execute a stored procedure called Exec sp_executesql, update the database, such as I modify the data after a row to get a statement " Exec sp_executesql n ' update [providerinfo] set [telno] = @p1 where (([id] = @p2) AND ([name] = @p3) AND (@p4 = 1 And [address] is null) OR ([address] = @p5)) AND (@p6 = 1 and [telno] is null) OR ([telno] = @p7)) AND (@p8 = 1 and [tradesum] is null) OR ([tradesum] = @p9)) AND ((@p10 = 1 and [remark] is null) OR ([remark] = @p11)) ) ', n ' @p1 varchar (3), @p2 int, @p3 varchar (2), @p4 int, @p5 varchar (3), @p6 int, @p7 varchar (4), @p8 int, @p9 int, @p10 int, @p11 varchar (3) ', @p1 = ', @p2 = 1, @p3 = ' WT ', @p4 = 0, @p5 = ' ABC ', @p6 = 0, @ p7 = ' 0000 ', @p8 = 0, @p9 = 8, @p10 = 0, @p11 = ' AAA ',
The new line statement is "exec sp_executesql N ' INSERT into [Providerinfo] ([name], [address], [Telno], [tradesum], [remark]) VALUES (@p1, @p2, @p3, @p4, @p5) ', N ' @p1 varchar (2), @p2 varchar (8000), @p3 varchar (8000), @p4 int, @p5 varchar (8000) ', @p1 = ' TT ', @p2 = NULL, @p3 = NULL, @p4 = NULL, @p5 = NULL "
This looks like a better understanding of the working mechanism of SqlDataAdapter, record the changes in the line, through the sqlcommanbuilder generation of the corresponding SQL statements, and then executed, to achieve the purpose of modification.
But I did some changes to the resulting data for the Multiple Tables Association, unfortunately, the modification is not successful, in fact, should not be successful, such as my student information in the field of the tutor is only the teacher's ID, and the display is based on the Mentor information Form associated with the real name of the tutor, if the user modified the name of the tutor, The response to the student table can not be the teacher's ID, the modification is not successful. Believe that there is a solution to this problem, but I have not found that should continue to study!
Finally, say a few words about today's events. 50 meters in physical education in the afternoon, in the case of a false shot to run a 6-second 2~ a bit fake ~ after class and freshman several college students play ~ to 5 O ' back to the bathhouse to bathe I really afraid I fell in the bathhouse ~ really have no strength ~ but with them playing the mood is very good, First of all, they play no food, hate and the food of the People dozen ~ Second they are full of vitality of the appearance let me seem to have returned to the freshman feeling ~ every day to play the life is really good! tomorrow morning English class, the word ~ ~ ~ ~ So tonight decided stay up coding!!! I'm glad I have some food.