How do I dynamically create a DataGridView in C#?
How do I dynamically crea开发者_开发问答te a DataGridView in C#? Could you please provide an example?
You can create it like any other controls.
place a PLACEHOLDER control in your page (this will serve as start point)
so your page looks like
<body>
<form id="form" runat="server" />
<asp:PlaceHolder id="ph" runat="server" />
</body>
Then, in your code behind, just create and add controls to the Place Holder
// Let's create our Object That contains the data to show in our Grid first
string[] myData = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
// Create the Object
GridView gv = new GridView();
// Assign some properties
gv.ID = "myGridID";
gv.AutoGenerateColumns = true;
// Assing Data (this can be a DataTable or you can create each column through Columns Colecction)
gv.DataSource = myData;
gv.DataBind();
// Now that we have our Grid all set up, let's add it to our Place Holder Control
ph.Controls.Add(gv);
Maybe you want to add more controls?
// How about adding a Label as well?
Label lbl = new Label;
lbl.ID = "MyLabelID";
lbl.Text = String.Format("Grid contains {0} row(s).", myData.Length);
ph.Controls.Add(lbl);
// Done!
Hope it helps get you started
Based on your reply that you are using WinForms. First a very simple example, then a little discussion of issues to consider based on "typical" usage scenarios.
Here's a specific example where, in response to a click on a button at run-time, a new DataGridView is created, positioned on the Form, sized, etc. :
// declare a form scoped variable to hold a reference
// to the run-time created DataGridview
private DataGridView RunTimeCreatedDataGridView;
// sample of creating the DataGridView at run-time
// and adding to the Controls collection of a Form
// and positioning and sizing
// fyi: the Form used here is sized 800 by 600
private void button1_Click(object sender, EventArgs e)
{
RunTimeCreatedDataGridView= new DataGridView();
RunTimeCreatedDataGridView.Size = new Size(300, 542);
RunTimeCreatedDataGridView.Location = new Point(10,12);
this.Controls.Add(RunTimeCreatedDataGridView);
}
You can, of course simplify setting Size and Location using the Bounds property, or the method 'SetBounds as in :
RunTimeCreatedDataGridView.SetBounds(10,12,300,542);
You may wish to set other properties that determine Size and Location "automatically" by setting the Dock or Anchor properties.
And you will probably want to "custom configure" the DataGridView's visual appearance in other ways via adding calls to set BackGroundColor, BorderStyle, etc. to the above code.
By this time, I'd hope you are thinking something like: "what about the really important things like configuring the columns, databinding, etc. ?" What about all that wonderful functionality exposed at DesignTime by the "Smart Tag" on the DataGridView's upper right corner, and in the Property Browser window.
Here's where we get general, rather than specific.
If you are "sure" that at some point the run-time user is going to want to create a DataGridView: why not create it in advance: visually style it, create columns, etc., and then hide it when the Form Loads: then show it on demand.
If you absolutely must create a DataGridView from scratch at run-time, but want to avoid a lot of typing : first create the DataGridView at design-time, go into the Designer.cs file and copy out the automatically generated code that is useful for you for visual style, adding and configuring columns: then paste that code in the method or event where you create the DataGridView (yes, you'll need to 'tweak it a bit).
Since, in this case, we know nothing about what you might, or might not, be binding the DataGridView to, we'll just stay "mum" on that one.
In (the odd ? off chance ?) the unlikely case you are creating multiple DataGridViews at run-time, suggest you maintain an internal list of them in a variable like List<DataGridView>
and have one variable named currentDataGridView
that you can rely on to hold a reference to the currently active (has focus, is visible, etc.) DataGridView.
In every case I recommend using "messing around" with a DataGridView in design-time mode, and then examining the code produced in the Designer.cs file (but never altering it !) to get quick information on how to use various features of the DataGridView. And for major examples of binding a DataGridView to complex DataSources and formatting: do check out CodeProject for relevant articles, tips, etc.
"Harvest" what you need from the automatically generated code in Designer.cs file, and then, when you are ready, delete the DataGridView instance on a Form, and "do your own thing" at run-time.
GridView gv = new GridView();
gv.datasource = dt;
gv.databind();
and then place this gv in panel or div or table coloumn.
You can check this link http://www.ehow.com/how_5212306_create-datagridview-c.html
精彩评论