简体   繁体   中英

I made a datatable. But it takes every time the first column of my database/datatable. How can I take the second row/column of it?

Can someone help me get the second row/column of my datatable because somehow it takes everytime the first one even if I say that I want the second one.

 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);


        }



    }

**I edited it so can someone help me? if I click on the picturebox then it will go to the positions of the datareader." But what I want is that if I click on the picturebox for the second time then it has to go to the new coordinates of the datatable. can you help me? **

Your DataTable could to be accessed in the form of array of muti-dimension . For example:

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

If you need can go all fields returned using two nested for statement:

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
     }
}

Instead you use:

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

use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM