You must Sign In to post a response.
  • Category: ASP.NET

    How to Delete Datarow from Dataset..

    Hi guys,

    I have to delete a Specific datarow from Datatable which is contains only alphabets. For an Example, in My datatable contains a Integer row. Unforutanly, in that a single row contains Alphabets instead of Integer. I have to delete that Row only. How to do that. Kindly guide me do that.
  • #762442
    Hi Karunanidhi,

    You can easily delete all the alphanumeric rows by using a linq method with a small function. Here's the complete code which I have implemented for you,


    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("SNo");
    dt.Columns.Add("InputData");

    DataRow dr;
    dr = dt.NewRow();
    dr["SNo"] = 1;
    dr["InputData"] = "tx";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["SNo"] = 2;
    dr["InputData"] = "1";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["SNo"] = 3;
    dr["InputData"] = "2";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["SNo"] = 4;
    dr["InputData"] = "txt";
    dt.Rows.Add(dr);

    int iFindIndex = 0;
    while (iFindIndex != -1)
    {
    iFindIndex = dt.Rows.OfType<DataRow>().ToList().FindIndex(t => IsAlphaNumeric(t["InputData"].ToString()) == true);

    if (iFindIndex != -1)
    {
    dt.Rows.RemoveAt(iFindIndex);
    dt.AcceptChanges();
    }
    }
    }
    }

    bool IsAlphaNumeric(string sValue)
    {
    bool isAlpha = false;
    try
    {
    int i = Convert.ToInt32(sValue);
    }catch(Exception ex)
    {
    isAlpha = true;
    }
    return isAlpha;
    }

    Regards,
    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."

  • #762450
    Hi

    You can try this code


    DataTable dt = new DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    dt.Columns.Add("Empno");
    DataRow dr;
    dr=dt.NewRow(();
    dr[0]=1;
    dr[1]="Asp.net";
    dr[2]=18500;
    dt.Rows.Add(dr);
    dt.Rows.Remove(0)//; Mention in your Datarow
    dt.Rows.RemoveAt(2) // Index postion


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments