简体   繁体   English

我做了一个数据表。 但是,它每次都需要我的数据库/数据表的第一列。 我该如何取第二行/列?

[英]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 . 您的DataTable可以以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语句返回所有字段:

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?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL,数据表。 为什么值在列的每一行中都会改变? - SQL,DataTable. Why value changing in every row in column? 我似乎无法检查数据表的特定列中的值。 为什么? - I can't seem to check a value in a particular column of a datatable. Why? 我想访问数据表的每个单元格。 怎么样? - I want to access the each cell of the datatable. How? 我试图弄清楚如何遍历我的数据表。 有什么建议吗? - I am trying to figure out how to loop through my datatable. Any Suggestion? C# 数据表。 比较一列中的数据,如果匹配,则追加相应行的另一列中的数据 - C# Datatable. Compare data from a column and if it matches, append the data from another column of the corresponding row 我有一个数据表。 我想在毛伊岛打印它。 我怎样才能做到这一点? 或者类似于 wpf 的 DataGrid - I have a DataTable. I want to print it on Maui. How can I do that? Or something similar to wpf's DataGrid 显示数据表。 我可以创建一个包含动态行数的表,但是如何在行内创建动态列数? - Showing a DataTable. I can make a table with a dynamical number of rows, but how to make a dynamical number of columns inside the rows? 我已经修改了数据表中的数据。 如何将修改后的数据放回表中? - I have modified data in a datatable. How do I get this modified data back into the table? 如何将条件数据从一个数据表添加到另一个数据表。 [错误:“位置0处无行”。C# - How to add conditional data from a datatable into another datatable. [error: " no row at position 0] . C# 无法序列化DataTable。未设置DataTable名称 - Cannot serialize the DataTable. DataTable name is not set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM