Skip to main content



.Net using windows form Datagridview?


1 comment

  • Avatar
    Jared Baszler

    We do this and it is pretty easy.  But it is based largely how your labels are designed and if they are tied to a single row in your data grid.  If each row represents one label the then you can specify a custom SQL statement for the label to only print the IDs in your database table that are selected in your data grid.  

    var dg = new DataGridView();

    // On the form where your data grid is - such as in a print button
    List<int> selectedIDs = new List<int>();
    foreach (var row in dg.SelectedRows)

    btFormat = btEngine.Documents.Open("path and file name of label document");
    string sqlCommand = "SELECT * FROM DatabaseTableName WHERE IDColumnName IN (";

    // Add any specific IDs to the SQL statement
    // The next line is using LINQ - if you can't use this then you need to loop through the List<int> and build the IN clause of the SQL statement
    sqlCommand = selectedIDs.Aggregate(sqlCommand, (current, id) => current + (id+ ","));  
    sqlCommand = sqlCommand.TrimEnd(',') + ") "; // Trim trailing comma and add the end parend
    sqlCommand += " ORDER BY SomeNameOfAColumn"; // This line is optional but slick if you need to order your labels a certain way

    ((OLEDB) btFormat.DatabaseConnections[0]).SQLStatement = sqlCommand;



Please sign in to leave a comment.