Introduction
In this article, I will examine how to create a BoundField
Column dynamically for a GridView control. The demonstration shows the ListBox
populated with Column Names from a specific table. Selecting multiple column
names from the ListBox will allow the user to display the data in the GridView
accordingly.
Requirements
·
Visual Studio 2005
·
Microsoft SQL Server 2000/2005
We will use the web.config file to store the
ConnectionString. In ASP.NET 2.0, a new configuration section called
<connectionStrings> allows us to store connection strings securely.
Listing 1 – Establishing Database Connectivity
<connectionStrings>
<add name="DummyDB" connectionString="Server=localhost;Integrated
Security=True;Database=DummyDB;Persist SecurityInfo=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
The connectionString property of SqlDataSource control is
assigned to the settings given in the web.config file. We will then populate
the ListBox control with the Column names from the Products table in the
Northwind Database as shown in Listing 2.
Listing 2 – Populate ListBox Control with the
ColumnNames
<asp:ListBox ID="ListBox1"AutoPostBack =true SelectionMode =Multiple
DataSourceID ="ListDataSource" DataTextField="COLUMN_NAME"
DataValueField ="COLUMN_NAME"runat="server">
</asp:ListBox>
<asp:SqlDataSourceID="ListDataSource" runat="server"
ConnectionString="<%$ConnectionStrings:DummyDB %>"
SelectCommand="select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNSwhere TABLE_NAME = 'Products'">
</asp:SqlDataSource>
Our goal is to display the GridView data based on the values
the user selects from the ListBox. So with a click of the button, we'll
populate the GridView control as shown in Listing 3.
Listing 3 – Display Data in GridView Control Based
on the Items Selected in ListBox
<asp:Button ID="Button1"runat="server" OnClick="Button1_Click"Text="Button" />
<asp:GridView ID="GridView1"runat="server"
DataSourceID="SqldataSource1" AutoGenerateColumns=false >
</asp:GridView>
<asp:SqlDataSourceID="SqlDataSource1" runat="server"
ConnectionString="<%$ConnectionStrings:DummyDB %>"
SelectCommand ="Select * fromProducts">
</asp:SqlDataSource>
The Boundfield members of the GridView control represent a
field that is displayed as text in a data-bound control. Based on the selection
in the ListBox, we'll iterate through selected ListBox items and create a BoundField.
We will set the DataField and HeaderText properties of the Boundfield, where DataField
gets or sets the name of the data field to bind to the BoundField object, and
HeaderText gets or sets the text that is displayed in the header of a data
control. BoundField being a column in the GridView, we will add it to the
GridView as shown in Listing 4.
Listing 4
[Visual Basic .NET]
<span class=Bold><span style='font-weight:normal'>ProtectedSub Button1_Click(ByVal sender As Object,</span></span>
<span class=Bold><span style='font-weight:normal'>ByVale As System.EventArgs)</span></span>
<span class=Bold><span style='font-weight:normal'>GridView1.Columns.Clear()</span></span>
<span class=Bold><span style='font-weight:normal'>Dimli As ListItem</span></span>
<span class=Bold><span style='font-weight:normal'>ForEach li In ListBox1.Items</span></span>
<span class=Bold> </span><span class=Bold><spanstyle='font-weight:normal'>If (li.Selected) Then</span></span>
<span class=Bold><span style='font-weight:normal'> 'Declare BoundField</span></span>
<span class=Bold><span style='font-weight:normal'> Dim bFld As BoundField = Nothing</span></span>
<span class=Bold><span style='font-weight:normal'> bFld = New BoundField()</span></span>
<span class=Bold><span style='font-weight:normal'> bFld.DataField = li.Text</span></span>
<span class=Bold><span style='font-weight:normal'> bFld.HeaderText = li.Text</span></span>
<span class=Bold><span style='font-weight:normal'> GridView1.Columns.Add(bFld)</span></span>
<span class=Bold><span style='font-weight:normal'> End If</span></span>
<span class=Bold><span style='font-weight:normal'>Next</span></span>
<span class=Bold><span style='font-weight:normal'>EndSub</span></span>
[C#]
protected void Button1_Click(object sender,EventArgs e)
{
GridView1.Columns.Clear();
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
BoundField bFld = null;
bFld = new BoundField();
bFld.DataField = li.Text;
bFld.HeaderText = li.Text;
GridView1.Columns.Add(bFld);
}
}
}
Downloads
[Download
Sample]
Conclusion
There are a lot more properties we can set to make this GridView
fit the application needs. The purpose of this article is to create a BoundField
Column of GridView based on the selected criteria. We can extend this sample to
set the Column formatting or sorting in a real-world scenario as well.