2012-11-12 2 views
0

DataTable의 데이터의 첫 번째 열을 텍스트 상자에 채우고 싶습니다.DataTable의 데이터로 텍스트 상자를 채우는 방법

구문을 어떻게 할 것입니까?

나는 지금까지 이것을 가지고 있었지만 마지막 줄에 오류가 발생했다.

DataTable dt = new DataTable(); 
    string strConnection = ConfigurationManager.ConnectionStrings["ChinatowndbConnString"].ConnectionString; 
    SqlConnection conn = new SqlConnection(strConnection); 
    string sql = "SELECT * FROM vwSchedule where scheduleid =" + scheduleid; 
    SqlCommand cmd = new SqlCommand(sql); 
    cmd.CommandType = CommandType.Text; 
    cmd.Connection = conn; 
    SqlDataAdapter sd = new SqlDataAdapter(cmd); 
    sd.Fill(dt); 

    tbEvent.Text dt.Rows[0].Field<String>(0); // first field of first row, assuming that it's a string 

감사 차 당신은 마지막 줄 구문에서 같음이 누락

+0

그래서 작동하지 않습니다. tbEvent.Text dt.Rows [0] .Field (0);을'tbEvent.Text = dt.Rows [0] .Field (0);으로 대체하면 예상대로 작동해야합니다. 행이고 첫 번째 열의 유형은 문자열). –

+0

coulmn 0은 문자열이 아닐 수 있습니다. – AMember

+0

실수로, = 기호를 잊어 버렸습니다. – TeaDrinkingGeek

답변

1

당신은

tbEvent.Text = dt.Rows[0].Field<String>(0); 

예상대로 작동합니다은 (행이 있고 첫 번째 열에의 타입이 문자열이라고 가정)으로

tbEvent.Text dt.Rows[0].Field<String>(0); 

를 교체합니다.

0

: 그것은 있었어야

tbEvent.Text dt.Rows[0].Field<String>(0); 

:

tbEvent.Text = dt.Rows[0].Field<String>(0); 

또는

tblEvent.Text = dt.Rows[0].ItemArray[0]; 

그렇게해야합니다.

참조 : Access cell value of datatable

관련 문제