I have a datagridview and it correctly displays the results according to the user input of a start date and end date.
I use the datagridview1.autogeneratecolumns = true to show all the fields.
I now need to show only specific fields but dont' want to edit the SQL.
I can add a column easily enough using datagridview1.Coumns.Add("colnName", "colHeader");
How do I bind data to 'colName'
If it helps, my code:
Expand|Select|Wrap|Line Numbers
- public void OpenDatabase(DateTime ddtpFrom, DateTime ddtpTo)
- {
- SqlConnection conn = new SqlConnection("Data Source=SBSHK; Initial Catalog=123; User ID=xxx; Password=xxx;");
- string strSQL = @"SELECT TOP (100) Glcode AS 代碼, Gldesc AS 描述, [Entry Number] AS 詞條數字, [Company Name] AS 公司名称, Mark AS 標記, Debit AS 借方, Credit AS 信用 FROM View_TransactionReport WHERE DATE BETWEEN @f AND @t AND dagbknr IS NOT NULL AND transtype <> 'V' AND (Debit IS NOT NULL AND Credit IS NOT NULL AND (Debit > 0 OR Credit > 0)) Order by [Entry Number]";
- SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
- DataSet ds = new DataSet();
- da.SelectCommand.Parameters.Add("@f", SqlDbType.DateTime).Value = ddtpFrom;
- da.SelectCommand.Parameters.Add("@t", SqlDbType.DateTime).Value = ddtpTo;
- try
- {
- conn.Open();
- int i = ds.Tables["dsVoucher"].Rows.Count;
- dataVoucher.AutoGenerateColumns = false;
- dataVoucher.Columns.Add("GlcodeCol", "Gl Code")
- //how do I bind Glcode from the SQL statement to GlcodeCol cells under the Gl Code header?
- dataVoucher.DataSource = ds;
- dataVoucher.DataMember = "dsVoucher";
- lbTotalRecords.Text = "Showing " + i + " records";
- }
- catch (Exception ex)
- {
- lbTotalRecords.Text = ex.ToString();
- }
- finally
- {
- conn.Close();
- }
- }