How do i get a particular cell from a datatable

Posted on 2007-10-16
Last Modified: 2008-01-09
I have a datatable and i want to grab the value of a cell and display it else where but i'm not sure how to go about it - could someone please help point it out...tha
Question by:mrperfect75000
    LVL 22

    Expert Comment

    You can get the value as:
    string content = yourDataTable.Rows[rowIndex][columnIndex].ToString();


    Author Comment

    what type is the rowindex? sorry for not understanding this... but i'm not sure how to get the rowindex and column index
    LVL 22

    Expert Comment

    They are just integers. Sy you want the value from the third row, 4th column, then:
    string content = yourDataTable.Rows[2][3].ToString();


    Author Comment

    Thanks for your help.

    This works, however, i wanted to get it programatically because i'm making a method that will be called several times.

    I have about 5 columns and many rows

    Here is what i'm trying to do...

    public static DataTable makeadatable(int ID)


    DataTable newdt = new DataTable();
            using (SqlConnection cnn = new SqlConnection(blahblah)
                using (SqlCommand cmd = new SqlCommand("mysp", cnn))
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@ID", id));
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);

            return newdt;


    Then i'm calling it like this....

    id = textbox1.text
    string name =  myclass.makeadatable(id).Rows[].[].ToString();

    what do i put in them programatically to get the appropriate name based on the ID?

    Thanks for your help!
    LVL 22

    Accepted Solution

    You need to change one bit:
    DataTable myData =  myclass.makeadatable(id);

    Now, what happens next depends on what you want to do with the content of the DataTable. If you want to iterate over the rows, and get the value from each of the five columns, you could do something like this:
    string col1Value = string.Empty;
    string col2Value = string.Empty;
    string col3Value = string.Empty;
    string col4Value = string.Empty;
    string col5Value = string.Empty;

    foreach (DataRow row in myData)
       col1Value = row[0].ToString();
       col2Value = row[1].ToString();
       col3Value = row[2].ToString();
       col4Value = row[3].ToString();
       col5Value = row[4].ToString();
       // Now, you need to do something with the strings before the content is overwritten by the next row.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now