跳至主內容

搜尋

搜尋

.Net using windows form Datagridview?

評論

1 條評論

  • 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)
    {
        selectedIDs.Add(int.Parse(row.Cells["ColumnNameOfID"]);
    }

    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;

     

    0

登入寫評論。