简体   繁体   中英

How do i read a row from a datatable in C#?

I'm looking for a simple way to read a row from a data table. I'm sure this has been answered somewhere but for the life of me i cant find it.

my code is as follows:

static void Main(string[] args)
        {
            DataTable table1 = new DataTable("myTable");
            DataColumn column;
            DataRow row;

        table1.PrimaryKey = new DataColumn[] { table1.Columns["idnum"] };

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.AutoIncrement = false;
        column.ColumnName = "name";
        column.ReadOnly = true;
        column.Unique = false;
        table1.Columns.Add(column);

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.AutoIncrement = false;
        column.ColumnName = "type";
        column.ReadOnly = true;
        column.Unique = false;
        table1.Columns.Add(column);

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.AutoIncrement = false;
        column.ColumnName = "atk";
        column.ReadOnly = true;
        column.Unique = false;
        table1.Columns.Add(column);

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.AutoIncrement = false;
        column.ColumnName = "spd";
        column.ReadOnly = true;
        column.Unique = false;
        table1.Columns.Add(column);



        row = table1.NewRow();

        row["name"] = "long sword";
        row["type"] = "weapon";
        row["atk"] = 7;
        row["spd"] = 4;
        table1.Rows.Add(row);

        row = table1.NewRow();

        row["name"] = "short sword";
        row["type"] = "weapon";
        row["atk"] = 5;
        row["spd"] = 5;
        table1.Rows.Add(row);

        row = table1.NewRow();

        row["name"] = "dagger";
        row["type"] = "weapon";
        row["atk"] = 3;
        row["spd"] = 8;
        table1.Rows.Add(row);

        foreach(DataRow rowly in table1.Rows)
        {
            //what goes here?

        }
        Console.ReadKey();

}

what do i write to see the data in the row?

You can print those values by using the following code:

int rowIndex=0;
StringBuilder rowValueBuilder = new StringBuilder();
foreach (DataRow rowly in table1.Rows)
{       
    rowValueBuilder.AppendFormat("Row {0} Values \n", rowIndex++);
    rowValueBuilder.AppendFormat("Name  : {0} \n", rowly["name"]);
    rowValueBuilder.AppendFormat("Type  : {0} \n", rowly["type"]);
    rowValueBuilder.AppendFormat("Atk   : {0} \n", rowly["atk"].ToString());
    rowValueBuilder.AppendFormat("Spd   : {0} \n", rowly["spd"].ToString());        
}
Console.WriteLine(rowValueBuilder.ToString());

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