问题
//...
{
public Form1()
{
InitializeComponent();
LoadData();
textBoxFill();
}
private void LoadData()
{
SqlConnection SCConnect = new SqlConnection("Server=localhost;Initial Catalog=T8;Integrated Security=SSPI;");
SCConnect.Open();
StringBuilder SBBuilder = new StringBuilder("Select * from Table8");
SqlDataAdapter SDA = new SqlDataAdapter(SBBuilder.ToString(), SCConnect);
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
DataTable DT = new DataTable();
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}
private void textBoxFill()
{
TextBox TB = new TextBox();
int A = 1;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
panel1.Controls.Add(TB);
TB.Location = new Point(10, (A * 20));
TB.Top = A * 28;
TB.Size = new Size(200, 50);
TB.Margin = new Padding(10, 10, 10, 10);
}
A = A + 1;
}
}
How do I add multiple TextBox follow by DataGridView.Columns.Count and each TextBox to fill in each DataGridView columns data.TQ?
回答1:
I am guessing after looking at the previous duplicate post, that this may be what you are looking for. It may help you if you explained the overall picture as this seems like on odd thing to do since the data is already in the grid and the user can edit it, I am not sure why you would do this data “duplication” in the panel.
However, it does appear you want to have the textboxes correspond to the currently “selected” row in the grid. Such that there will be one textbox for each column in the grid. Initially, you do know how many columns the data may contain. Therefore, you need to dynamically create the textbox’s in the panel.
One approach to “bind” each textbox to a column of the currently selected row in the grid may be accomplished by “binding” each textbox to a particular column in the DataTable
that is used as the DataSource
to the grid. Each textbox has a property called…DataBindings
. This property will allow you to “bind” the textbox to a particular column in the DataTable
. Below is an example.
To help, given we have the data, I suggest a method AddTextBoxesToPanel(DataTable dt)
… that takes a DataTable
and loops through the columns of that table and creates a textbox for each column AND adds the “binding” for that column to that textbox. With this approach, no extra code will be necessary to fill the text boxes when the user selects different rows.
private void AddTextBoxesToPanel(DataTable dt) {
panel1.Controls.Clear();
panel1.AutoScroll = true;
panel1.AutoScrollMinSize = new Size(0, (dt.Columns.Count * 23) + 15);
TextBox curTB;
int y = 10;
foreach (DataColumn col in dt.Columns) {
curTB = GetTextBox(10, y);
curTB.DataBindings.Add(new Binding("Text", dt, col.ColumnName));
panel1.Controls.Add(curTB);
y += 23;
}
}
Above, we assume this may be called more than once and need to “clear” any previous textboxs in the panel. Set the panel to be scrollable, then start the loop through the columns to add the textboxes to the panel. The GetTextBox
method (below) simply gets a new TextBox
with the desired location. Lastly, we set the DataBinding
for “that” textbox to point to “that” column. curTB.DataBindings.Add(new Binding("Text", dt, col.ColumnName));
private TextBox GetTextBox(int xLoc, int yLoc) {
TextBox TB = new TextBox {
Text = "",
Location = new Point(xLoc, yLoc),
Size = new Size(150, 50),
Margin = new Padding(10),
Anchor = AnchorStyles.Left
};
return TB;
}
Below is a complete example using the above method. The Forms Load
method to fill a DataTable
with 10 columns and 20 rows, then use that DataTable
as a DataSource
to the grid. Then call the method above to set the textboxes into the panel.
private void Form1_Load(object sender, EventArgs e) {
FillGrid(10, 20);
AddTextBoxesToPanel((DataTable)dataGridView1.DataSource);
}
A method to generate some data for testing.
private void FillGrid(int totalColumns, int totRows) {
DataTable dt = new DataTable();
// add columns
for (int i = 0; i < totalColumns; i++) {
dt.Columns.Add("Col" + i, typeof(string));
}
// add rows
object[] data = new object[totalColumns];
for (int row = 0; row < totRows; row++) {
for (int col = 0; col < totalColumns; col++) {
data[col] = "Col" + col + "Row" + row;
}
dt.Rows.Add(data);
}
dataGridView1.DataSource = dt;
}
Hope this helps.
来源:https://stackoverflow.com/questions/60815767/how-to-add-multiple-textbox-follow-by-datagridview-columns-count-and-column-data