Get specific column foreach row in DataTable

1950

How can I get specific column from each row in DataTable. In this example I want to get top 3 news and I got problem with Image. This code gives allways a image from first row, because I defined dr.Rows[0]["Image"]. How can i change this to get right image for every row?

SqlCommand cmd = new SqlCommand("SELECT TOP 3 Title, Content, Date, Autor, Category, Image FROM News", con);
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);

    foreach (DataRow row in dt.Rows)
    {
        Panel panel = new Panel();
        panel.Attributes.Add("class", "post");

        HtmlGenericControl header = new HtmlGenericControl("h2");
        header.InnerHtml = row["Title"].ToString();
        header.Attributes.Add("class", "naslovPosta");
        panel.Controls.Add(header);

        HtmlGenericControl date = new HtmlGenericControl("p");
        date.InnerHtml = ((DateTime)row["Date"]).ToString("dd.MM.yyyy.");
        date.Attributes.Add("class", "datumPosta");
        panel.Controls.Add(date);

        Image img1 = new Image();
        img1.ImageUrl = ResolveUrl("~/Images/" + dt.Rows[0]["Image"]);
        panel.Controls.Add(img1);

        HtmlGenericControl content = new HtmlGenericControl("pre");
        content.InnerHtml = row["Content"].ToString();
        panel.Controls.Add(content);

        pnlNews.Controls.Add(panel);
    }
Tim Schmelter

You already enumerate all rows, you find the current row in the row-variable, so replace:

img1.ImageUrl = ResolveUrl("~/Images/" + dt.Rows[0]["Image"]);

with

img1.ImageUrl = ResolveUrl("~/Images/" + row["Image"]);

like you did it already f.e. at row["Title"].ToString();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

datatable get specific column from row

From Dev

foreach DataTable row select unique at second column

From Dev

Add a row under specific column in datatable

From Dev

Set value of each DataTable row in specific column

From Dev

Get Values from DataTable by row and column name

From Dev

Get specific column header names from DataTable

From Dev

How to prevent row click function on specific column in datatable?

From Dev

Scroll to specific row in DataTable

From Dev

Get a specific value from a specific column from specific row?

From Dev

Datatable function for specific column

From Dev

Datatable foreach detail/child row

From Dev

How to get a specific column value from a DataTable in c#

From Dev

How to get one specific column from datatable into model in MVC Razor

From Dev

How to get specific column value for a specific row value in AWK?

From Dev

Get the value of a specific row and specific column from datagrid

From Dev

How to get sum of specific column at specific row range in SQL Server?

From Dev

Get Column name of specific row and matching column in Excel

From Dev

Get Column name of specific row and matching column in Excel

From Dev

Foreach loop through Datatable Column

From Dev

Find row in datatable with specific id

From Dev

Find row in datatable with specific id

From Dev

Delete a specific row from datatable

From Dev

Finding a specific value in a datatable row

From Dev

Get datatype of column in datatable

From Dev

Get datatype of column in datatable

From Dev

Get SUM of DataTable column

From Dev

Dropdown filter on specific column in DataTable

From Dev

get column name that matches specific row value in dataframe

From Dev

Get the COUNT() in row specific case (the counts will depend on the column)

Related Related

  1. 1

    datatable get specific column from row

  2. 2

    foreach DataTable row select unique at second column

  3. 3

    Add a row under specific column in datatable

  4. 4

    Set value of each DataTable row in specific column

  5. 5

    Get Values from DataTable by row and column name

  6. 6

    Get specific column header names from DataTable

  7. 7

    How to prevent row click function on specific column in datatable?

  8. 8

    Scroll to specific row in DataTable

  9. 9

    Get a specific value from a specific column from specific row?

  10. 10

    Datatable function for specific column

  11. 11

    Datatable foreach detail/child row

  12. 12

    How to get a specific column value from a DataTable in c#

  13. 13

    How to get one specific column from datatable into model in MVC Razor

  14. 14

    How to get specific column value for a specific row value in AWK?

  15. 15

    Get the value of a specific row and specific column from datagrid

  16. 16

    How to get sum of specific column at specific row range in SQL Server?

  17. 17

    Get Column name of specific row and matching column in Excel

  18. 18

    Get Column name of specific row and matching column in Excel

  19. 19

    Foreach loop through Datatable Column

  20. 20

    Find row in datatable with specific id

  21. 21

    Find row in datatable with specific id

  22. 22

    Delete a specific row from datatable

  23. 23

    Finding a specific value in a datatable row

  24. 24

    Get datatype of column in datatable

  25. 25

    Get datatype of column in datatable

  26. 26

    Get SUM of DataTable column

  27. 27

    Dropdown filter on specific column in DataTable

  28. 28

    get column name that matches specific row value in dataframe

  29. 29

    Get the COUNT() in row specific case (the counts will depend on the column)

HotTag

Archive