Two ways of binding data to a GridView (ASP.NET)

In the following example, I will show you how to bind the same field to an ASP.NET GridView using two different methods.

In the first example, I utilize the an asp:BoundField to display the data, whereas in the second example I utilize an asp:TemplateField

Example 1 – BoundField:

[vb]

<asp:BoundField DataField=”ORDER_NUMBER” HeaderText=”Order” ReadOnly=”True” SortExpression=”ORDER_NUMBER” ItemStyle-Wrap=false />

[/vb]

Example 2 – TemplateField:

[vb]

<asp:TemplateField HeaderText=”Order” SortExpression=”OrderLineSched” ItemStyle-Wrap=”false”>
<ItemTemplate>
<asp:Label ID=”OrderLineSched” runat=”server” Text='<% #Bind(“ORDER_NUMBER”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

[/vb]

Both will display exactly the same, however the TemplateField provides greater flexibility because if, for example, you change the field type from Label to TextBox, you can make it an editable field that will actually be editable in the entire GridView (all rows).

Export Data Grid (datagridview) to Excel in VB.NET

Create the following method and pass it the DataGridView that you want to export.  This will export all grid contents (including column headers) to Excel and “freeze” the top row.

Note: The “prog_Progress” object is a ProgressBar I added to the application to display the progress of the export to the user.

[csharp]

private void ExportDataGridViewToExcel(DataGridView dgv)
{
// exit method if there are no records to export
if (dgv.RowCount == 0)
{
MessageBox.Show(“No data to export.”, “No Data”, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}

SaveFileDialog fd = new SaveFileDialog();

fd.OverwritePrompt = false;
fd.FileName = “Export-” + DateTime.Now.ToString(“yyyyMMdd.hhmm”) + “.xls”; // default file name
fd.Filter = “Excel (*.xls) |*.xls;*.xlsx”;

if (fd.ShowDialog() == DialogResult.OK) // only continue if user specified/selected a file
{
UpdateStatus(“Exporting data to Excel…”);

// create the Excel app
Microsoft.Office.Interop.Excel._Application xlApp = new Microsoft.Office.Interop.Excel.Application();

// create the Workbook
Microsoft.Office.Interop.Excel._Workbook xlWB = xlApp.Workbooks.Add();

// create the Worksheet
Microsoft.Office.Interop.Excel._Worksheet xlWS = (Microsoft.Office.Interop.Excel._Worksheet)xlWB.Worksheets[1];

try
{
// show the progress bar
prog_Progress.Visible = true;
prog_Progress.Style = ProgressBarStyle.Blocks;
prog_Progress.Maximum = dgv.RowCount – 1;

// export the column headers
for (int c = 0; c < dgv.ColumnCount; c++)
{
xlWS.Cells[1, c + 1] = dgv.Columns[c].HeaderText;
}

// bold and underline the first row
Microsoft.Office.Interop.Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)xlWS.Rows[1];
rng.EntireRow.Font.Bold = true;
rng.EntireRow.Font.Underline = true;

// freeze the top row
xlApp.ActiveWindow.SplitRow = 1;
xlApp.ActiveWindow.FreezePanes = true;

// export the data in the DataGridView
for (int r = 0; r <= dgv.RowCount – 1; r++)
{
Application.DoEvents(); // prevent the app from “Not Responding”…
prog_Progress.Value = r; // update the Progress Bar

for (int c = 0; c <= dgv.ColumnCount – 1; c++)
{
xlWS.Cells[r + 2, c + 1] = dgv[c, r].Value;
}
}

xlWS.Columns.AutoFit(); // autofit the columns

xlWB.SaveAs(fd.FileName); // save the file

prog_Shares.Visible = false;

xlWB.Close();
xlWS = null;
xlWB = null;
xlApp.Quit();
fd = null;

MessageBox.Show(“Excel Workbook Created Successfully”, “Export Complete”, MessageBoxButtons.OK, MessageBoxIcon.Information);
UpdateStatus(“Export to Excel complete!”);
}
catch (Exception ex)
{
xlWB.SaveAs(fd.FileName); // save the file
xlApp.Quit(); // quit Excel
prog_Progress.Value = prog_Shares.Maximum;
MessageBox.Show(“Error exporting data to Excel.” + Environment.NewLine + ex.Message, “Export error”, MessageBoxButtons.OK, MessageBoxIcon.Error);
prog_Shares.Visible = false;
}
}
}

[/csharp]

Show current line number in multiline textbox (C#)

I use the KeyUp and MouseUp events to trigger a quick lookup to show the current line number where the insertion point is in a multiline textbox.  In the following example, my multiline textbox is called txt_FileContents and I wrote a single method (UpdateCurrentLineNumber) that fetches the current line number, the total number of lines in the textbox, and then updates a label (lbl_LineNumber) to show the information.  Happy coding!

[csharp]

void txt_FileContents_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
UpdateCurrentLineNumber();
}

void txt_FileContents_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
UpdateCurrentLineNumber();
}

private void UpdateCurrentLineNumber()
{
int index = this.txt_FileContents.SelectionStart;
int line = this.txt_FileContents.GetLineFromCharIndex(index) + 1;

lbl_LineNumber.Text = ” Ln ” + line.ToString(“N0″) + ” / ” + this.txt_FileContents.Lines.Count().ToString(“N0”);
}

[/csharp]

Detect Double Right Mouse Click (VB.NET)

Simple Black Ops 2 countdown timer that’s triggered/dipslayed when the user double right-clicks the status bar in an application.  #easteregg

[vbnet]

Private Sub StatusStrip1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles StatusStrip1.MouseDoubleClick
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim dt_BO2Launch As Date = “11/13/2012 00:00:00”
Dim tspan As TimeSpan = dt_BO2Launch.Subtract(Now)
MessageBox.Show(tspan.Days & “:” & tspan.Hours & “:” & tspan.Minutes & “:” & tspan.Seconds & vbCrLf & “DD:HH:MM:SS”, “Time until Black Ops 2 Launch!”, MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
End Sub

[/vbnet]

Increase/Decrease Textbox Font Size Programatically (VB.NET)

[vbnet]
Private Sub frm_Main_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
‘ Ctrl + + = increase the SQL size
If e.Control And e.KeyCode = Keys.Add Then
Using f As Font = TextBox1.Font
TextBox1.Font = New Font(f.FontFamily, f.Size + 1, f.Style)
End Using
Exit Sub
End If

‘ Ctrl + – = decrease the SQL size
If e.Control And e.KeyCode = Keys.Subtract Then
Using f As Font = TextBox1.Font
TextBox1.Font = New Font(f.FontFamily, f.Size – 1, f.Style)
End Using
Exit Sub
End If
End Sub
[/vbnet]

Capture when Ctrl+Enter keys are pressed at the same time (VB.NET)

Make sure your Form’s KeyPreview property is set to True.

[vbnet]
Private Sub frm_Main_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.Enter Then
‘ Do something
End If
End Sub
[/vbnet]

You can also capture when the Alt+Enter keys are pressed at the same time with the following:

[vbnet]
Private Sub frm_Main_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Alt And e.KeyCode = Keys.Enter Then
‘ Do something
End If
End Sub
[/vbnet]

Populate a DataGridView with SqlDataReader

In order to display the data in a SqlDataReader object in a DataGridView control, you first need to load the data into a DataTable object. The code below is in C# and the Connection String is for a SQL Server database connection.

[csharp]
using System.Data.SqlClient;

SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;

// set the connection string
con.ConnectionString = “Data Source=SqlServerName;Initial Catalog=DbName;Integrated Security=True”
con.Open();

string SQL = “SELECT * FROM tbl_Users”;

// create the SQL command
cmd = new SqlCommand(SQL, con);

// execute the SQL
dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

DataTable table = new DataTable();
table.Load(dr);

reader.Close();
reader.Dispose();
con.Close();
con.Dispose();
cmd.Dispose();
cmd = null;

// Display the data in the DataGridView control…
DataGridView1.DataSource = table;
[/csharp]

How to fetch data from using a BackgroundWorker (VB.NET)

BackgroundWorkers (which I will refer to as bgWorker) are great for doing background processing in your application, thus preventing your program from going into a “Not Responding” status.  One downfall, however, is that you cannot pass or reference data in a separate thread from the bgWorker.  The following code shows how to get around this by utilizing Global/Public variables because they are referenced between all threads of the application.


Global/Public Variables

[vbnet] ‘ public variables
Public dr As OdbcDataReader
Public conn As OdbcConnection

Dim miliSecond As Long = 1000
Dim timerSeconds As Long
Dim startDttm As Date
Dim endDttm As Date
Dim sqlComm As String
Dim dt As New DataTable
Dim da As New OdbcDataAdapter
Dim strConnString As String
Dim comm As OdbcCommand
[/vbnet]
How to call the BackgroundWorker
[vbnet]
bgWorker = New BackgroundWorker
bgWorker.WorkerReportsProgress = True
bgWorker.WorkerSupportsCancellation = True
bgWorker.RunWorkerAsync() ‘ run the background worker
[/vbnet]
Run the BackgroundWorker and fetch the data
[vbnet]
Private Sub FetchData(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgWorker.DoWork
Try
Dim conn As New OdbcConnection(strConnString)
startDttm = Now()

comm = New OdbcCommand(sqlComm)
comm.Connection = conn
comm.Connection.Open()

dt = New DataTable
da = New OdbcDataAdapter
da.SelectCommand = comm ‘ Utilize the SELECT statement
da.Fill(dt) ‘ Fill the Data Table

Catch ex As Exception
MsgBox(“ERROR: ” & ex.Message, MsgBoxStyle.Critical, “Error running SQL”)
dbClose()
End Try
End Sub
[/vbnet]
What to do when the BackgroundWorker is finished
[vbnet]
Private Sub bgWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
Try
UpdateDataGrid()

Catch ex As Exception
bgWorker.CancelAsync()
End Try
End Sub
[/vbnet]
Update the DataGridView (dg)
[vbnet]
Private Sub UpdateDataGrid()
Try
Dim intCount As Integer
intCount = dt.Rows.Count ‘ number of rows returned by SQL

Select Case intCount
Case Is < 1000 ‘ fill if less than 1000 rows
Me.dg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dg.DataSource = dt ‘ Populate the Data Grid with the Data Table values
dr = comm.ExecuteReader
Case Else
If MsgBox(“This will display ” & Format(dt.Rows.Count, “###,###,##0″) & ” rows. Would you like to continue?”, MsgBoxStyle.YesNo, “Continue?”) = MsgBoxResult.Yes Then
Me.dg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dg.DataSource = dt ‘ Populate the Data Grid with the Data Table values
dr = comm.ExecuteReader
End If
End Select

dbClose()

dg.AutoResizeColumns()

If comm.ExecuteNonQuery >= 0 Then
intCount = comm.ExecuteNonQuery
End If

AddToSummary(Format(intCount, “###,###,##0″) & IIf(intCount = 1, ” record found”, ” records found”), False)

Dim dur As Global.System.TimeSpan = Now.Subtract(startDttm)
AddToSummary(” – ” & dur.Seconds & “.” & dur.Milliseconds & ” seconds”, True)

Catch ex As Exception
MessageBox.Show(“Error updating data grid”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
[/vbnet]
Close the Database Connection

[vbnet]
Private Sub dbClose()
Try ‘ Close the Data Reader
dr.Close()
dr = Nothing
Catch ex As Exception
‘MsgBox(“Error closing the data reader” & vbCrLf & ex.Message, MsgBoxStyle.Critical, “Data Reader Close Issue”)
End Try

Try ‘ Close the Connection
conn.Dispose()
conn = Nothing
Catch ex As Exception
MsgBox(“Error closing the connection” & vbCrLf & ex.Message, MsgBoxStyle.Critical, “ODBC Connection Close Issue”)
End Try
End Sub
[/vbnet]
Add to Summary method – I use a TextBox (txt_Status) on my Form for displaying application status to the user
[vbnet]
Private Sub AddToSummary(ByVal strText As String, ByVal Append As Boolean)
If Append Then
Me.txt_Status.Text += strText
Else
Me.txt_Status.Text = strText
End If
End Sub
[/vbnet]