I am getting a error when I am trying to get a specific column from my table in the datagridview.
Here is how I populate the table----
public DataTable createGridForForm(int rows, int columns)
{
// Create the output table.
DataTable table = new DataTable();
for (int i = 1; i <= columns; i++)
{
table.Columns.Add("column " + i.ToString());
}
for (int i = 1; i < rows; i++)
{
DataRow dr = table.NewRow();
// populate data row with values here
table.Rows.Add(dr);
}
return table;
}
And here is how i create the datagridview------
private void createGridInForm(int rows, int columns)
{
DataGridView RunTimeCreatedDataGridView = new DataGridView();
RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
ID_Column.Width = 200;
int positionForTable = getLocationForTable();
RunTimeCreatedDataGridView.Size = new Size(800, 200);
RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
myTabPage.Controls.Add(RunTimeCreatedDataGridView);
}
The error I am getting is that the Index was out of range. It may not be negative and must be smaller than the size. What I am trying to do is that I'm getting a table from a text file and then in run time I am showing it in my form, but the table doesn't match my data grid view in size, it doesn't look good. So I want to make the table fit the Data grid view.
Try-
DataGridViewColumn ID_Column = dataGridView1.Columns[0];
ID_Column.Width = 200;
Related
//...
{
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?
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.
What is the main reason of this error
No individual rows in this collection can be addressed because the
table contains vertically linked cells. '
I have table in which I want to add data (stored in array) but second row of the table is merged and I got this error message.
What can be appropriate solution to remove this error.
Code :
if (pCell.Range.Text.Contains("List of components robots"))
{
iDT16 = 0; rowcount = 0;
foreach (int Row in wordDocument.Tables[j].ToString()) // Add row in the table according to data available inside Result array
{
while (sDesignation_Componentsrobots[iDT16] != null)
{
wordDocument.Tables[j].Rows.Add();
wordDocument.Tables[j].Rows.SetHeight(28, Word.WdRowHeightRule.wdRowHeightAtLeast);
rowcount = wordDocument.Tables[j].Rows.Count;
iDT16++;
}
}
// Fill the row with data and add checkbox in particular table column.
iDT16 = 0; iEST16 = 0;
for (int row = 3; row <= rowcount; row++)
{
wTable.AllowAutoFit = true;
wTable.Rows[row].Range.Font.Bold = 0;
wTable.Rows[row].Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wTable.Rows[row].Range.ParagraphFormat.SpaceBefore = 5;
wTable.Cell(row, 1).Range.Text = sDesignation_Componentsrobots[iDT16];
wTable.Cell(row, 3).Range.Text = sEmergency_stopcircuit_T16[iEST16];
Word.FormField checkBox2 = wTable.Cell(row, 6).Range.FormFields.Add(wTable.Cell(row, 6).Range, Word.WdFieldType.wdFieldFormCheckBox);
iDT16++; iEST16++;
}
break;
}
Table with merged row:
I have a dynamically created WPF Grid (rows and columns count based on layout) like this:
private Grid CreateGrid(string layout)
{
int col;
int row;
if (layout == "4x4")
{
row = 4;
col = 4;
}
else
{
row = 2;
col = 2;
}
Grid output = new Grid();
for (int i = 0; i < col; i++)
{
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
output.ColumnDefinitions.Add(coldef);
}
for (int i = 0; i < row; i++)
{
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridLength.Auto;
output.RowDefinitions.Add(rowdef);
}
return output;
}
In each of these "cells" should be another object, filled with data from dataSet (one table per object). That object is also another set of grids. My aim is to loop through this dynamically created grid and put this object into each cell. (and if there is layout 2x2 but only 3 tables in the dataset, I want only 3 object there and last cell would be empty)
However, I did not find anything like "foreach column in grid", or even how to get column / row count. On the other hand, all I found was related to DataGrid or GridView - what is the difference, and if the other type would be better to use for this, why?
Thanks in advance
So, I was able to find a solution to this. It might not be the best one, but it works.
Here is the code:
foreach (DataTable table in result.Tables)
{
GfoTopBox box1 = StuffData(table);
Grid.SetRow(box1, j);
Grid.SetColumn(box1, i);
output.Children.Add(box1);
i++;
if (i >= output.ColumnDefinitions.Count)
{
i = 0;
j++;
}
}
"result" is the data set, each table provides the data for one of the objects I wanted to put in the cells of the Grid in the question (output).
However, if anyone has better solution, I am open to suggestions :)
my DataTable has over 1000 columns and I want to display values on the datagridview. Because of the FillWeigth problem I use the following method to fill the gridview,
public bool TransferDataTableToGrid(DataGridView dataGrid, DataTable dataTable)
{
dataGrid.SuspendLayout();
if ((dataGrid != null) && (dataTable != null))
{
dataGrid.Columns.Clear();
dataGrid.AutoGenerateColumns = false;
dataGrid.DataSource = dataTable;
for (int i = 0; i < dataTable.Columns.Count; i++)
{
DataGridViewColumn column = new DataGridViewColumn();
column.Name = dataTable.Columns[i].ColumnName;
column.FillWeight = 1;
column.CellTemplate = new DataGridViewTextBoxCell();
column.ValueType = dataTable.Columns[i].DataType;
dataGrid.Columns.Add(column);
}
for (int i = 0; i < dataTable.Columns.Count; i++)
{
for (int ii = 0; ii < dataTable.Rows.Count; ii++)
{
dataGrid[i, ii].Value = dataTable.Rows[ii][i];
}
}
}
dataGrid.ResumeLayout();
return true;
}
and sometimes I have an effect that my gridview is empty. Only after second execution data is displayed. Do you have any ideas, why...?
Thanks.
I recommend to use paging, i mean that you can show about 20 columns with navigation buttons
under your grid, it's like Google or others... even your are not programming a web application.
Use binding source to fill your grid
SqlDataAdapter adapter = new SqlDataAdapter(database.cmd);
dataSet1.Tables.Clear();
adapter.Fill(dataSet1, "Table");
bs = new BindingSource();
bs.DataSource = dataSet1.Tables["Table"];
dataGridView1.DataSource = bs;
now you dont need to worry about creating columns and fill cells in loops and its much better performance
Bind Data to Datagridview
Well, I solved my problem. With Ivan's suggestion I tried the alternative way to fill data: instead of using DataSource I add new rows manually
foreach (DataRow row in dataTable.Rows)
{
var dataGridRow = new DataGridViewRow();
dataGridRow.CreateCells(dataGrid);
for (int i = 0; i < row.ItemArray.Length; i++)
{
dataGridRow.Cells[i].Value = row.ItemArray[i];
}
dataGrid.Rows.Add(dataGridRow);
}
...and it works - data in dgv is displayed. Thanks!
I've been trying to generate a table with n number of rows. Being used to PHP makes this all the worst. I tried the following code:
using System.Data;
// Create a DataTable instance
DataTable dTbl = new DataTable("myDynamicTable");
// Create a DataColumn instances
DataColumn dValue = new DataColumn();
DataColumn dMember = new DataColumn();
dValue.ColumnName = "Id";
dValue.DataType = Type.GetType("System.Int32");
dMember.ColumnName = "Name";
dMember.DataType = Type.GetType("System.String");
// Add these DataColumns into the DataTable
dTbl.Columns.Add(dValue);
dTbl.Columns.Add(dMember);
DataRow myrow = dTbl.NewRow();
myrow["Id"] = 1;
myrow["Name"] = "Tux";
// Add the row into the table
dTbl.Rows.Add(myrow);
but nothing displayed. Any idea why?
All I need is to display a table with 3 columns and n number of rows. This number will of rows will be dependent on number of records in database satisfying a certain conditions.
I also tried this:
HtmlTable table1 = new HtmlTable();
// Set the table's formatting-related properties.
table1.Border = 1;
table1.CellPadding = 3;
table1.CellSpacing = 3;
table1.BorderColor = "red";
// Start adding content to the table.
HtmlTableRow row;
HtmlTableCell cell;
for (int i = 1; i <= 5; i++)
{
// Create a new row and set its background color.
row = new HtmlTableRow();
row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
for (int j = 1; j <= 4; j++)
{
// Create a cell and set its text.
cell = new HtmlTableCell();
cell.InnerHtml = "Row: " + i.ToString() +
"<br>Cell: " + j.ToString();
// Add the cell to the current row.
row.Cells.Add(cell);
}
// Add the row to the table.
table1.Rows.Add(row);
}
// Add the table to the page.
this.Controls.Add(table1);
but it didn't work!
Instead of doing "this.Controls.Add(table1)" add the table to the .aspx page, and then modify it through the code.
Even better - use a databound GridView.