I at vb.net (2012), the form has a text box and datagridview control that want to realize the function: through the text box input a number, the automatically generated in the datagridview corresponding number of lines, also need the work that is automatically generated at the same time, in automatic add two buttons on the right side line, an input text box number, will automatically generate, on the same line will appear two buttons, the basic realization, rendering is as follows:
The code is as follows: 'text box content change events Private Sub TextBox1_TextChanged (sender As Object, As EventArgs e) Handles TextBox1. The TextChanged Dim iLoops As Integer DataGridView1. Rows. The Clear () If TextBox1. Text="" Then MsgBox (" please enter the number of rows!" , MsgBoxStyle. Critical + MsgBoxStyle. OkOnly) The Exit Sub End the If ILoops=CInt (TextBox1. Text) Dim As Integer I For I=1 To iLoops DataGridView1. Rows. The Add (CStr (I), ""," ", "", Guid. NewGuid (), ToString ()) Next AddButton () End Sub 'add two buttons Private Sub AddButton () Dim btn1 As DataGridViewButtonColumn=New DataGridViewButtonColumn () Dim btn2 As DataGridViewButtonColumn=New DataGridViewButtonColumn () Btn1. The HeaderText="" Btn1. Text="increase" Btn1. Name="btnSet1 "Btn1. Width=70 Btn1. UseColumnTextForButtonValue=https://bbs.csdn.net/topics/True Btn2. The HeaderText="" Btn2. Text="delete" Btn2. Name="btnSet2 "Btn2. Width=70 Btn2. UseColumnTextForButtonValue=https://bbs.csdn.net/topics/True DataGridView1. Columns. The Add (btn1) DataGridView1. Columns. The Add (btn2) End Sub Enter a valid for the first time, but when the second time in the text box input line number is right, the right of the button will increase, has been increased, The following effect:
Excuse me, how to solve: when any change text box, line number increasing, the button in the column is only two columns of buttons, rather than always increase,
CodePudding user response:
You have a business process problem: 1. According to you to do so, need to bind a separate list for each button click event 2. Put the generated code text_change, although you can run, but there will be a very serious problem of wrong operation: A. ten lines of input, for example, your code will run twice; B. Enter the number of illegal, your code doesn't check, 3. The datagridview needs is set to "no", allows you to add "is not allowed to delete"
You can be adjusted according to the following steps: 1. Manual editing datagridview columns, a total of four, two textbox, two button columns: "edit", "value", "rights to add button", "delete button", the "edit" and "value" two columns of data binding, binding field for the "edit" and "value"; 2. The datagridview is set to "are not allowed to add", "is not allowed to delete" 3. Add a button behind the textbox, put the business code on the button click event, and add the validity check
if not integer. Tryparse (textbox1. Text, out rowcount as integer) then 'tryparse function of vb write I'm not sure, see IDE prompts yourself Messagebox. Show (" input "illegal) The exit sub End the if Dim table as new DataTable Table. The columns. The add (" number ") Table. The columns. The add (" value ") 'here is a for loop in the table. The rows. The add (1, guid newguid (), tostring (), add the column of Datagridview1. The datasource=table. Defaultview 'the binding table of the default view to the datagridview
4. In datagridview cellclick event, for the rights to add and delete write code, through the "e.r ow to figure out which line, ordered by the olumn of e.c. with our fabrication: determine whether point" rights to add "or" delete "
if "e.r ow> 0 then If the olumn=2 of e.c. with our fabrication: then 'add button code Elseif olumn=3 then of e.c. with our fabrication: 'delete button code End the if End the if