简体   繁体   中英

Customize gridview c# windows application

My Table has 10 columns fetched from a database, while I need to bind only 4 columns of it to a DataGridView in that 4 column 3 columns from database one of them should add dynamically(This column not in database) extra Windows application c#

string sql1 = "Select * From Table";
        SqlConnection connection1 = new SqlConnection(constring);
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql1, connection1);
        DataSet ds1 = new DataSet();
        connection1.Open();
        dataadapter.Fill(ds1, Reporttype_tbl1);
        connection1.Close();
        dataGridView2.DataSource = ds1;
        dataGridView2.DataMember = Reporttype_tbl1;    

If you need extra processing beyond the data that you have in your table you could:

(Imagine that you want to calculate the revenue, giving the income and expenses of a business)

  1. Use the query itself to do the extra processing for these columns that are not in the database.

For example: SELECT Name, Income, Expenses, Revenue = Income - Expenses FROM TABLE

In this case, the Revenue will be calculated as the query runs.

  1. Use your C# code to do the extra processing and change your datasource to the new one.

For example:

  • Add a Revenue column to your DataTable
  • Make the formula for each line
  • Use the new DataTable as DataSource

In this case, you are adding the data after the query has been completed, using your C# code to the calculations.

Picking the right one depends on whats kind of work you are wanting to do and what you feel more comfortable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM