.Net using windows form Datagridview? フォローする

1
Avatar
Ben Roland

Does anyone have an example in VB or C# where you can multi-select rows in a datagridview and push that out to a bartender document? 

I image it would be something like:

btFormat.SubStrings("Job_Number").Value = datagridview.column.xxxx

Also not sure how the multi-select would be coded.

Thanks!

1 コメント

0
Avatar
Jared Baszler
コメントアクション Permalink

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;

 

ログインしてコメントを残してください。