How to iterate through a DataTable

后端 未结 4 1440
死守一世寂寞
死守一世寂寞 2020-11-29 19:51

I need to iterate through a DataTable. I have an column there named ImagePath.

When I am using DataReader I do it this way:

4条回答
  •  囚心锁ツ
    2020-11-29 20:33

    The above examples are quite helpful. But, if we want to check if a particular row is having a particular value or not. If yes then delete and break and in case of no value found straight throw error. Below code works:

    foreach (DataRow row in dtData.Rows)
            {
                if (row["Column_name"].ToString() == txtBox.Text)
                {
                    // Getting the sequence number from the textbox.
                    string strName1 = txtRowDeletion.Text;
    
                    // Creating the SqlCommand object to access the stored procedure
                    // used to get the data for the grid.
                    string strDeleteData = "Sp_name";
                    SqlCommand cmdDeleteData = new SqlCommand(strDeleteData, conn);
                    cmdDeleteData.CommandType = System.Data.CommandType.StoredProcedure;
    
                    // Running the query.
                    conn.Open();
                    cmdDeleteData.ExecuteNonQuery();
                    conn.Close();
    
                    GetData();
    
                    dtData = (DataTable)Session["GetData"];
                    BindGrid(dtData);
    
                    lblMsgForDeletion.Text = "The row successfully deleted !!" + txtRowDeletion.Text;
                    txtRowDeletion.Text = "";
                    break;
                }
                else
                {
                    lblMsgForDeletion.Text = "The row is not present ";
                }
            }
    

提交回复
热议问题