How to create multiple gridviews?
My code:
GridView gv = new GridView();
Panel PNl = new Panel();
for (int i = 0; i < DST.Tables.Count; i++)
{
gv.ID = DST.Tables[i].TableName;
gv.DataSource = DST.Tables[i];
gv.DataBind();
// form.Controls.Add(gv);
PNl.Controls.Add(gv);
}
//gv.Parent.Controls.Add(form);
form.Attributes["runat"] = "server";
form.Controls.Add(PNl);
this.Controls.Add(form);
However, my code is creating only one grid view.
Move the creation of the GridView inside the loop. This way you will create many grids and not just reuse one object. In practice you probably want to create a user control with the grid and use that since you will be able to specify different grid attributes in markup which is easier than specifying them in code.
int i=10;
GridView[] gc = new GridView[i];
for (int i = 0; i < 10; i++)
{
gc[i] = new GridView();
gc[i].ID = "gv" + i;
gc[i].CopyBaseAttributes(GridView1);
gc[i].AlternatingRowStyle.CopyFrom(GridView1.AlternatingRowStyle);
gc[i].BorderStyle = GridView1.BorderStyle;
gc[i].ControlStyle.CopyFrom(GridView1.ControlStyle);
gc[i].EditRowStyle.CopyFrom(GridView1.EditRowStyle);
gc[i].EmptyDataRowStyle.CopyFrom(GridView1.EmptyDataRowStyle);
gc[i].FooterStyle.CopyFrom(GridView1.FooterStyle);
gc[i].HeaderStyle.CopyFrom(GridView1.HeaderStyle);
gc[i].RowStyle.CopyFrom(GridView1.RowStyle);
gc[i].SelectedRowStyle.CopyFrom(GridView1.SelectedRowStyle);
gc[i].ShowHeaderWhenEmpty = true;
gc[i].RowDataBound += new GridViewRowEventHandler(Gv_RowDataBound);
//gc[i].EmptyDataText = "No ROWS Found in the particular month";
}
Have GridView1 in design page and set visible to false, so you can copy properties.
Related
I have a WindowsForms application that I am compiling as a DLL to be used by another program. This program supplies the DLL with an array filled with information(floats). What I want to do is display these numbers in an organized way(row and columns). The methods I've investigated so far incorporate the use of a DataGridView object, however I do not have a "database", I only have the raw data stored as an array.
Would the only way to do this involve creating a SQL database with my data from the array? Or is there an easier/faster way of doing this?
Thanks for the replies, but I just ended up looping through the creation of the rows and columns and filling them with my required data.
Creating/initializing DataGridView:
private System.Windows.Forms.DataGridView dataGridView1;
this.dataGridView1 = new System.Windows.Forms.DataGridView();
//Size and location can be changed to whatever you want
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 436);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 70; //Adjusts row header to fit entire "Score" string
this.dataGridView1.Size = new System.Drawing.Size(976, 398);
this.dataGridView1.TabIndex = 10;
Creating rows/columns and filling them with my data:
for (int i = 0; i <= 40; i++)
{
dataGridView1.Columns.Add("column" + i.ToString(), i.ToString());
dataGridView1.Columns[i].Width = 22;
}
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
for (int i = 0; i <= 40; i++)
{
row.Cells[i].Value = scores[0,i]; // "scores" is an Int32[,] array filled with my data
}
dataGridView1.Rows.Add(row);
dataGridView1.Rows[0].HeaderCell.Value = "Score";
The result looked something like this.
You can send the gridview data in XML format
private void Form1_Load(object sender, EventArgs e) {
DataSet ds = new DataSet();
ds.ReadXml(#"XMLFile1.xml");
gridControl1.DataSource = ds.Tables[1];
gridView1.PopulateColumns();
}
I am developing a software and i need different form of a DataGridView.
I created them and inserted them into an array with this method:
private DataGridView[] cloneDataGridViews(int posCount, DataGridView dataGridView)
{
List<DataGridView> dataGridViewList = new List<DataGridView>();
for(int i=0;i<posCount;i++)
{
DataGridView dgv = new DataGridView();
dgv = dataGridView;
dataGridViewList.Add(dgv);
}
return dataGridViewList.ToArray();
}
And I am trying to show them with this code:
void GridViewSelectorLoad(object sender, System.EventArgs e)
{
this.AutoScroll = true;
int startY = 30;
for(int i=0;i<dataGridViewArray.Length;i++)
{
int height = dataGridViewArray[i].Height;
int posY = startY + 10 + i*height;
Panel pnl = new Panel();
pnl.Controls.Add(dataGridViewArray[i]);
dataGridViewArray[i].Parent = pnl;
pnl.Location = new Point(100,posY);
pnl.Name = "pnl"+i.ToString();
pnl.Height = dataGridViewArray[i].Height;
pnl.Width = dataGridViewArray[i].Width;
pnl.Parent = this;
this.Controls.Add(pnl);
}
}
But it shows just one datagridview, how can I show all of them?
What is wrong with that code?
A control can only have one parent, but you're trying to set the same DataGridView as a child of multiple Panels.
for(int i=0;i<posCount;i++)
{
DataGridView dgv = new DataGridView();
dgv = dataGridView; // not creating a new instance of DataGridView
dataGridViewList.Add(dgv);
}
Here's the relevant part of the Controls.Add() method that causes this behavior.
if (value.parent != null)
{
value.parent.Controls.Remove(value);
}
You've got a single instance of DataGridView that you add to each new Panel. Each time, it's Parent property is set to the latest Panel. Then when you try adding it to the next Panel, the code above removes it from the previous one.
If you create a new instance inside the loop, it works fine. You'll need to copy over those values from the existing DataGridView that you wish to have in each new DataGridView instance.
for(int i=0; i<posCount; i++)
{
DataGridView dgv
= new DataGridView
{
Name = dataGridView.Name,
DataSource = dataGridView.DataSource,
...
};
dataGridViewList.Add(dgv);
}
cloneDataGridViews is not cloning the datagrid, its adding the same instance multiple times:
dgv = dataGridView;
It has been pointed out that all you are copying in your code is the same old reference to the same DGV into each slot of your list. Instead you will have to copy both the column structure and the values of all cells like this:
public DataGridView cloneDataGridView(DataGridView oldDGV)
{
DataGridView newDGV = new DataGridView();
foreach (DataGridViewCell cell in oldDGV.Rows[0].Cells)
newDGV.Columns.Add(new DataGridViewColumn(cell));
newDGV.Rows.Add(oldDGV.Rows.Count);
for (int row = 0; row < oldDGV.Rows.Count; row++)
for (int col = 0; col < oldDGV.Columns.Count; col++)
newDGV[col, row].Value = oldDGV[col, row].Value;
return newDGV;
}
You could call it like this:
for(int i=0;i<posCount;i++)
{
DataGridView dgv = cloneDataGridView(dataGridView);
dataGridViewList.Add(dgv);
}
Note: This piece of code assumes that there is at least one row in the source DGV.
I have searched for a while but can never get a clear answer on the correct way of doing this, or even if it is possible.
I am grabbing 2 rows from a table in SQL, returning them in a DataTable. I iterate through the rows and dynamically create a div for that row and then a label for each column with the stored value, and then repeats the process for the next row.
All I am missing to make it work is storing the labels into a List to bring back to the placeholder that the div will be created in.
here is a snippet... also, I want to do it this way, not with a gridview or tables for learning purposes and I already know how to use the gridview and table to do this. I have a total of 7 columns in this SQL table, and could have an unlimited number of rows.
EDIT
public void AddDiv(DataTable gameData)
{
for (int i = 0; i < gameData.Rows.Count; i++)
{
//newControl.InnerHtml = AddLabel(gameData, i);
//PlaceHolder1.Controls.Add(newControl);
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "div" + i++;
Label lblTitle = new Label();
lblTitle.Text = gameData.Rows[i]["Game_Title"].ToString();
this.Controls.Add(lblTitle);
PlaceHolder1.Controls.Add(lblTitle);
Label lblPublisher = new Label();
lblPublisher.Text = gameData.Rows[i]["Game_Publisher"].ToString();
this.Controls.Add(lblPublisher);
PlaceHolder1.Controls.Add(lblPublisher);
Label lblGenre = new Label();
lblGenre.Text = gameData.Rows[i]["Game_Genre"].ToString();
this.Controls.Add(lblGenre);
PlaceHolder1.Controls.Add(lblGenre);
Label lblESRB = new Label();
lblESRB.Text = gameData.Rows[i]["Game_ESRB"].ToString();
this.Controls.Add(lblESRB);
PlaceHolder1.Controls.Add(lblESRB);
Label lblUserRating = new Label();
lblUserRating.Text = gameData.Rows[i]["Game_UserRating"].ToString();
this.Controls.Add(lblUserRating);
PlaceHolder1.Controls.Add(lblUserRating);
Label lblWebsite = new Label();
lblWebsite.Text = gameData.Rows[i]["Game_Website"].ToString();
this.Controls.Add(lblWebsite);
PlaceHolder1.Controls.Add(lblWebsite);
}
}
First off, the InnerHtml property of the HtmlGenericControl is a string. Your code is assigning the result of a void method to this property. I think what you want to do is create the div and pass a reference to that to the AddLabel method. Here you can create your labels and add them to the div's Control's property. Finally, add your div to the Placeholder as you currently do. Hopefully, this will get you on the right track.
protected void Page_Load(object sender, EventArgs e)
{
AddDiv();
}
public void AddDiv()
{
for (int i = 0; i < 5; i++)
{
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "div" + i;
AddLabel(newControl, i);
PlaceHolder1.Controls.Add(newControl);
}
}
protected void AddLabel(HtmlGenericControl control, int i)
{
Label lblTitle = new Label();
lblTitle.Text = "label" + i.ToString();
control.Controls.Add(lblTitle);
Label lblPublisher = new Label();
lblPublisher.Text = "publisherLabel" + i.ToString();
control.Controls.Add(lblPublisher);
}
I am trying to do something like this:
for (int i = 1; i < nCounter ; i++)
{
string dvName = "dv" + i.ToString();
System.Windows.Forms.DataGridView dvName = new DataGridView();
// other operations will go here..
}
As you can guess, what I am trying to do is at i == 1, create a DataGridView with name dv1, and at i == 2, create a DataGridView with name dv2, but I can't.
Visual studio squiggles saying "a local variable named dvName is already delared in this scope" I also tried the following:
for (int i = 1; i <nCounter ; i++)
{
System.Windows.Forms.DataGridView dv & i = new DataGridView();
// other operations will go here..
}
But VS squiggles again, I hope you understood what I am trying to accomplish. Can anyone suggest how can I do this?
What you really need is a Dictionary<int, DataGridView> grids. Populate it in your for loop (grids[i] = new DataGridView();) and then, later, use the required grid (grids[someCalculatedIndex])
Hope this helps.
try a data structure where you can hold your variables eg dict etc
System.Collections.Generic.Dictionary<string,System.Windows.Forms.DataGridView>
grids = new Dictionary<string,System.Windows.Forms.DataGridView>();
for (int i = 1; i <nCounter ; i++)
{
grids.Add("dv" + i.ToString(), new DataGridView());
}
// to work on grid 1
DataGridView grid1 = grids["dv1"];
// so on
So your are trying to create the variable name dynamically? That's not possible. Why not use an Array or a List (or even a Dictionary)? Or do you want to just set the name of the control?
var list = new List<DataGridView>();
for (int i = 1; i <nCounter ; i++)
{
System.Windows.Forms.DataGridView dvName = new DataGridView();
dvName.Name = "dv" + i.ToString();
list.Add(dvName);
// other operations will go here..
}
foreach (var dv in list)
{
...do something...
}
DataGridView secondDv = list.Single(dv=>dv.Name == "dv2");
secondDv.DoSomething()
Not clear want you want to do...
private void CreateNewControl()
{
List<Control> list = new List<Control>();
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
this.Controls.Add(layout);
layout.ColumnCount = 3;
layout.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
for (int i = 0; i < 9; i++)
{
if (wantedType == DevExpress.XtraEditors.CheckEdit)
{
CheckBox chk = new CheckBox();
chk.Tag = i;
layout.Controls.Add(chk);
layout.AutoScroll = true;
}
if (wantedType == LabelControl)
{
Label chk = new Label();
chk.Tag = i;
layout.Controls.Add(chk);
layout.AutoScroll = true;
}
// I want to set the columnwidth of the layout so that when the labels are displayed they do not get clustered and look exactly like when displaying the checkboxes.How do I do it?
In general, what I do is:
Use the IDE in a 'prototype' project, to create a form with the controls in the positions that I want
Look at the source code created by the IDE (in the MyFormName.Designer.cs file) to see what source code is generated by the IDE to creat these controls
Create my own form in my real project, with hand-coded code that's based on what I learned from the prototype which I created using the IDE
// Loop through all the controls you want to add.
// Add a integer field that measures the highest width of each control like
int _iMaxWidth = 0;
for (int i=0; i < TotalControls.Count; ++i)
{
if ( control[i].Width > _iMaxWidth)
_iMaxWidth = control[i].Width
}
// Then you'll know what the width size of the column should be.
Col.Width = iMaxWidth + 2; // +2 to make things a little nicer.