Today, is a little simple but very helpful piece of code in both c#and vb.net. This is a demonstration of how to show the footer and bind a blank row to a DataGrid that has been binded form the SQLDataSource.
Firstly, in the page load, enter the following code at the end of all other code.
In c#
int rowcount = GridView1.Rows.Count;
if (rowcount == 0)
{bindCount();
}
and in vb.net
Dim rowcount As Integer = GridView1.Rows.Count
If rowcount = 0 Then
bindCount()
End If
Then create the following void to create a false row and show the footer.
void bindCount()
{
GridView1.DataSourceID = null;
GridView1.DataSource = null;
DataTable dt = new DataTable();
string FirstC = "episode_id";
DataColumn dCol1 = new DataColumn(FirstC, typeof(System.String));
string SecondC = "code";
DataColumn dCol2 = new DataColumn(SecondC, typeof(System.String));
string ThirdC = "value";
DataColumn dCol3 = new DataColumn(ThirdC, typeof(System.String));
string FourthC = "description";
DataColumn dCol4 = new DataColumn(FourthC, typeof(System.String));
string FifthC = "theTimestamp";
DataColumn dCol5 = new DataColumn(FifthC, typeof(System.String));
dt.Columns.Add(dCol1);
dt.Columns.Add(dCol2);
dt.Columns.Add(dCol3);
dt.Columns.Add(dCol4);
dt.Columns.Add(dCol5);
for (int i = 0; i <= 0; i++) {
DataRow row1 = dt.NewRow();
row1[FirstC] = "";
row1[SecondC] = "";
row1[ThirdC] = "";
row1[FourthC] = "";
row1[FifthC] = "";
dt.Rows.Add(row1);
}
foreach (DataColumn col in dt.Columns) {
BoundField bField = new BoundField();
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}
GridView1.DataSource = dt;
GridView1.Columns[6].Visible = false;
GridView1.Columns[7].Visible = false;
GridView1.Columns[8].Visible = false;
GridView1.Columns[9].Visible = false;
GridView1.Columns[10].Visible = false;
GridView1.DataBind();
}
and in vb.net
Private Sub bindCount()
GridView1.DataSourceID = Nothing
GridView1.DataSource = Nothing
Dim dt As New DataTable()
Dim FirstC As String = "episode_id"
Dim dCol1 As New DataColumn(FirstC, GetType(System.String))
Dim SecondC As String = "code"
Dim dCol2 As New DataColumn(SecondC, GetType(System.String))
Dim ThirdC As String = "value"
Dim dCol3 As New DataColumn(ThirdC, GetType(System.String))
Dim FourthC As String = "description"
Dim dCol4 As New DataColumn(FourthC, GetType(System.String))
Dim FifthC As String = "theTimestamp"
Dim dCol5 As New DataColumn(FifthC, GetType(System.String))
dt.Columns.Add(dCol1)
dt.Columns.Add(dCol2)
dt.Columns.Add(dCol3)
dt.Columns.Add(dCol4)
dt.Columns.Add(dCol5)
For i As Integer = 0 To 0
Dim row1 As DataRow = dt.NewRow()
row1(FirstC) = ""
row1(SecondC) = ""
row1(ThirdC) = ""
row1(FourthC) = ""
row1(FifthC) = ""
dt.Rows.Add(row1)
Next
For Each col As DataColumn In dt.Columns
Dim bField As New BoundField()
bField.DataField = col.ColumnName
bField.HeaderText = col.ColumnName
GridView1.Columns.Add(bField)
Next
GridView1.DataSource = dt
GridView1.Columns(6).Visible = False
GridView1.Columns(7).Visible = False
GridView1.Columns(8).Visible = False
GridView1.Columns(9).Visible = False
GridView1.Columns(10).Visible = False
GridView1.DataBind()
End Sub
This example has been produced on a datagrid that has 5 columns. Depending how many columns your datagrid has, increase or decrease the number of columns accordingly. This code need to take in to consideration any invisible columns you may have.
No comments:
Post a Comment