2015-07-05 28 views
1

有人可以帮助我得到我的数据表的第二行/列,因为不知何故它每次都需要第一个,即使我说我想要第二个。我做了一个数据表。但是它每次都需要我的数据库/数据表的第一列。我怎样才能把它的第二行/列?

int score = 1; 
    private void pbBier_Click(object sender, EventArgs e) 
    { 
     MySqlConnection conn = new MySqlConnection("Server=localhost;Database=patn4lj1;Uid=root;Pwd=root;"); 

     conn.Open(); 

     MySqlCommand command = conn.CreateCommand(); 
     command.CommandText = "select * from locaties"; 
     MySqlDataReader reader = command.ExecuteReader(); 

     DataTable dtData = new DataTable(); 
     dtData.Load(reader); 

       //lblX.Text = rowX["X"].ToString(); 


     int xPos1 = (Int32)dtData.Rows[0][0]; //dit is de 4de rij van de 1ste kollom.  // lblX.Text = rowX[colX].ToString(); 
     int xPos2 = (Int32)dtData.Rows[1][0]; 
     int xPos3 = (Int32)dtData.Rows[2][0]; 
     int xPos4 = (Int32)dtData.Rows[3][0]; 
     int xPos5 = (Int32)dtData.Rows[4][0]; 

     int yPos1 = (Int32)dtData.Rows[0][1]; 
     int yPos2 = (Int32)dtData.Rows[1][1]; 
     int yPos3 = (Int32)dtData.Rows[2][1]; 
     int yPos4 = (Int32)dtData.Rows[3][1]; 
     int yPos5 = (Int32)dtData.Rows[4][1]; 
     // DataColumn colY = dtData.Columns[1]; 
     // DataRow rowY = dtData.Rows[4];   // lblY.Text = rowY["Y"].ToString();   
     // int YPos = rowY.ToString()[4];   // lblY.Text = rowY[colY].ToString(); 

     lblAantalScore.Text = score++.ToString(); 



     bool Gedaan = false; 

     while (Gedaan == false) 
     {    
      pbBier.Location = new Point(xPos1, yPos1); 

      if (pbBier.Left == xPos1 && pbBier.Top == yPos1) 
      { 
       Gedaan = true; tmLoop.Stop(); 
       MessageBox.Show("gefeliciteerd u hebt er " + lblTijd.Text + " Sec over gedaan"); tmLoop.Start(); 
      } 


      if (Gedaan == true) 
      { 
       pbBier.Location = new Point(xPos2, yPos2); 
      } 

     //pbBier.Location = new Point(xPos3, yPos3); 
     //pbBier.Location = new Point(xPos4, yPos4); 
     //pbBier.Location = new Point(xPos5, yPos5); 


     } 



    } 

**我编辑过,所以有人可以帮我吗?如果我点击图片框,那么它会进入数据记录器的位置。“但是我想要的是,如果我第二次点击图片框,那么它必须到达数据表的新坐标。 ?我**

+0

的DataRow行= dtData.Rows [1]会给你的第二行。它基于0。对象数据= dtData.Rows [1] [1]会给你第二列。 – Saragis

+0

看看我的新编辑的代码,我希望我的图片框移动,当我点击第一次,但是当我点击第二次它必须去Xpos和Ypos的协调,如果你知道我的意思。我希望你可以帮助我? – Wanthelp

+0

您的问题仍然让我困惑,主要目标是什么?首先根据哪些位置移动图画框?第二次点击对我而言还不清楚?你可以更好地澄清这些疑惑,所以我可以帮助你? –

回答

0

取而代之您使用:

Object data = dtData.Rows[3][1]; 
int xPos = data.ToString()[1]; 

使用:

int xPos = (Int32)dtData.Rows[3][0];//you know which access fourth row and first column? 
+0

非常感谢你,你真的帮助我。你很棒!!! – Wanthelp

+0

标记为答案,谢谢! –

+0

嘿,现在我有一个问题,因为我说:“如果我点击图片框,那么它会去datareader的位置。”但我想要的是,如果我第二次点击图片框,那么它必须转到数据表的新坐标。你可以帮我吗? – Wanthelp

2

DataTable能在array of muti-dimension形式进行访问,例如:

dtData.Rows[0][0]; // access the first row and first column 
dtData.Rows[0][1]; // access the first row and second column 
dtData.Rows[0][2]; // access the first row and third column 
dtData.Rows[1][0]; // access the second row and first column 
dtData.Rows[1][1]; // access the second row and second column 
dtData.Rows[1][2]; // access the second row and third column 

如果你需要可以去所有领域使用两个嵌套for语句返回:

for(int i = 0;i < dtData.Rows.Count;i++)//travels the rows 
{ 
    for(int j = 0;j < dtData.Rows.Count;j++)//travels the columns 
    { 
      var valueField = dtData.Rows[i][j];//access the value of current field 
    } 
} 
+0

很好的答案。给予好评。 –

+0

如果你解决了你的问题,请选择一个答案。 –

+0

但是,当我输入这个然后是显示我这个错误错误:只能分配,调用,增加,减少,等待,和新的对象表达式可以用作语句\t D:\ Programeren \ blokboek4 \ week8 \ ZoetebierBlendiAO1C2 \ ZoetebierBlendiAO1C2 \ Form1 .cs ZoetebierBlendiAO1C2 – Wanthelp

相关问题