Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As volody pointed out, you can use the SelectedRows property to get everything you need. However, you might not know the indexes of the columns in question (although they're usually generated in the same order as the query, so make sure to alias the columns properly), you can use the column text itself via string access on the cells:</p> <pre><code>foreach(DataGridViewRow row in DataGridView1.SelectedRows) { // Do something with row.Cells["CustID"].Value; // Do something with row.Cells["ContractID"].Value; // Do something with row.Cells["TotalPayment"].Value; } </code></pre> <p>The other thing I would suggest you do is to leave the formatting up to the DataGridView instead of the SQL query. This is generally best practice when separating your data access/business logic layer from your presentation/UI layer. The way you would do this would be to specify a <code>DataFormatString</code> on the DataTable's DataColumn for the columns in question. In this particular case, you would do this:</p> <pre><code>... DataTable dt = new DataTable(); dt.Load(dr); dt.Columns["TotalPayment"].DataFormatString = "C"; dt.Columns["MonthlyInstallment"].DataFormatString = "C"; dt.Columns["TotalCommission"].DataFormatString = "C"; dataGridView1.DataSource = dt; </code></pre> <p>The downside to doing everything in code is that you don't get any design-time support, meaning you have to test everything out during run-time (which could be time consuming). That's one of the reasons ORM's are used (such as the VS DataSet Designer, LINQ2SQL designer, EntityFramework, etc), so that you have design-time support of database queries/objects. It makes it even easier to bind these data classes directly to UI controls at design-time and determine everything about the presentation layer before even running the code (such as which columns will be hidden, any additional computed columns, special column formatting, conditional row/column/cell painting, etc).</p> <p>Just my $0.02 worth.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload