2013-08-07 88 views
2

例如,我有這個表修改數據表

EmployeeName  EmpoyeeID 
John Mark  60001 
Bent Ting  60002 
Don Park  60003 

我如何可以顯示僱員有在數據表中的前導星號? 示例:* 60001 * 60002 * 60003

public DataTable ListOfEmployee() 
    { 
     DataSet ds = null; 
     SqlDataAdapter adapter; 
     try 
     { 
      using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) 
      { 

       myDatabaseConnection.Open(); 
       using (SqlCommand mySqlCommand = new SqlCommand("Select * Employee", myDatabaseConnection)) 
       { 
        ds = new DataSet(); 
        adapter = new SqlDataAdapter(mySqlCommand); 
        adapter.Fill(ds, "Users"); 
       } 
      } 
     } 

     catch (Exception ex) 
     { 
      throw new Exception(ex.Message); 
     } 
     return ds.Tables[0]; 
    } 

我需要顯示在水晶報表數據表與員工ID前導星號

public void Employees() 
    { 
     ReportDocument rptDoc = new ReportDocument(); 
     Employees ds = new Employees(); // .xsd file name 
     DataTable dt = new DataTable(); 

     // Just set the name of data table 
     dt.TableName = "Employees"; 
     dt = ListOfEmployee(); //This function is located below this function 
     ds.Tables[0].Merge(dt); 

     string strReportName = "Employees.rpt"; 
     string strPath = Application.StartupPath + "\\Reports\\" + strReportName; 
     // Your .rpt file path will be below 
     rptDoc.Load(strPath); 

     //set dataset to the report viewer. 
     rptDoc.SetDataSource(ds); 

     ReportViewer newReportViewer = new ReportViewer(); 
     newReportViewer.setReport(rptDoc); 
     newReportViewer.Show(); 
    } 
+0

有很多方法可以做到這一點,每個方法都是「最好的」方式,具體取決於在查詢出數據後你將如何處理數據。你能解釋更多(通過編輯原始問題)你查詢數據後對數據做什麼? –

+0

顯示它在哪裏?這個問題不是很清楚,爲什麼你不加星號?例如。 string astData =「*」+ table.GetField (「EmpoyeeID」)。ToString(); –

+0

最好的解決方案是在消耗數據的地方附加星號,而不是在數據表中。 – Khan

回答

2

最簡單不是最好的辦法是以該格式獲取數據,當且僅當它不打算用於其他任何地方時。您可以在您的查詢中執行此操作

Select '*'+id,col1,col2,col3 from employee 

雖然最好的方法是在使用它時修改列值。但顯然比單純在查詢中添加更令人頭疼。

1

將其添加到Crystal報表本身。

喜歡的東西 -

"*" & {tblTable.FieldName} 

1

沒有測試,但在你的函數與最終替換return語句(雖然我不記得語法Crystal報表,對不起!)下面的代碼應該工作:

DataTable table = ds.Tables[0]; 
DataTable clonedTable = table.Clone(); 
clonedTable.Columns["EmployeeID"].DataType = typeof(String); 
foreach (DataRow row in table.Rows) 
{ 
    clonedTable.ImportRow(row); 
} 
foreach (DataRow row in clonedTable.Rows) 
{ 
    row["EmployeeID"] = "*" + row["EmployeeID"].ToString(); 
} 
return clonedTable; 

然而,正如其他人所說,我會建議地方添加asterick向下行,當數據被讀取,而不是到TABL e本身。