iT邦幫忙

0

C#foreach指令問題

c#

載入A資料庫修改欄位裡面的值,但無法逐項修改此欄位值有人給我foreach這個KEY WORD但是無法達到目的,請問站內高手哪裡出錯了嗎?

附上CODING以便查看:
try
{
using (SqlConnection connAMHS = new SqlConnection
("Data Source=10.16.95.210 ; initial Catalog=AMHS ; User ID=kydbo; Password =P@$$w0rd0;"))
using (SqlCommand cmdAMHS = new SqlCommand())
{
if (connAMHS.State == ConnectionState.Closed)
connAMHS.Open();
cmdAMHS.Connection = connAMHS;
string szsql = String.Format(@"select A.*, B.* from AMHS_Bobbin A, AMHS_CarouselMap B
where A.PGroupID=5 and A.PDeviceTypeID in (15,16,25)
and A.PMainDeviceID = B.MainDeviceID and A.CSubDeviceID = B.SubDeviceID
and A.PCarouselPeg = B.Peg");

cmdAMHS.CommandText = szsql;

DataTable dt = new DataTable();
using (SqlDataReader reader = cmdAMHS.ExecuteReader())
dt.Load(reader);
this.dataGridView3.DataSource = dt;

foreach (DataRow DRow in dt.Rows)
{

object[] RowArray;
RowArray = DRow.ItemArray;

string AMHS25 = RowArray[25].ToString();

if (AMHS25 != "")
{
if (AMHS25 == "1")
RowArray[25] = "601";
if (AMHS25 == "2")
RowArray[25] = "602";
if (AMHS25 == "3")
RowArray[25] = "603";
DRow.ItemArray = RowArray;

this.dataGridView3.Rows.Add(DRow.ItemArray);

fillano iT邦超人 1 級 ‧ 2015-03-06 14:15:30 檢舉
if (AMHS25 != "")
{
    if (AMHS25 == "1")
        RowArray[25] = "601";
    if (AMHS25 == "2")
        RowArray[25] = "602";
    if (AMHS25 == "3")
        RowArray[25] = "603";
    this.dataGridView3.Rows.Add(RowArray);
}
else
{
    this.dataGridView3.Rows.Add(DRow.ItemArray);
}

隔空抓藥,討論就好XD
外獅佬 iT邦大師 1 級 ‧ 2015-03-06 16:12:03 檢舉
大師好~大師還沒睡~開心
這問題....小弟真的看不懂發問者想表達什麼汗

1 個回答

0
外獅佬
iT邦大師 1 級 ‧ 2015-03-10 12:10:06
最佳解答

有必要把ItemArray特地拿出來處理嗎??疑惑
通常直接讀取、修改DataRow的欄位值就好了說....落寞

<pre class="c" name="code">
foreach (DataRow DRow in dt.Rows)
{
  string AMHS25 = DRow[25].ToString();
  if (AMHS25 != "")
  {
    if (AMHS25 == "1")
      DRow[25] = "601";
    if (AMHS25 == "2")
      DRow[25] = "602";
    if (AMHS25 == "3")
      DRow[25] = "603";
  }
}
dataGridView3.DataSource = dt;
dataGridView3.DataBind();

我要發表回答

立即登入回答