1

what I have done with a grid view is .. According to ma database i have added columns to the datatable and after that i binded it to a gridview.,

here is the code,

/// Get Activities here to bind to GridView To Get the Activities at first Column.

    Dt = BlObj.BlDynamic_Table("[USP_DynamicGridView]", 2);
    DtOperation = BlObj.BlDynamic_Table("[USP_DynamicGridView]", 1);
    for (int i = 0; i < DtOperation.Rows.Count; i++)
    {
        Dt.Columns.Add(DtOperation.Rows[i][0].ToString());
    }
    dgrDynamic.DataSource = Dt;
    dgrDynamic.DataBind();

but for me needed is to get the column index.. here is the code

private int GetColumnIndexByName(int p)

{
    return ((int)GetColumnName(BlObj.BlDynamic_Table("[USP_DynamicGridView]", 
    4, p).ToString()));
}

private int GetColumnName(string name)
{
    foreach (DataColumn col in dgrDynamic.Columns)
    {
        int Index = 0;
        if(col.Equals(name.ToLower().Trim()))
       // if (col.Name.ToLower().Trim() == name.ToLower().Trim())
        {
            return Index;
        }
        Index += 1;
    }

    return -1;
}

What the problem is the foreach loop is not working..

I'm a fresher to .NET and I also don't know whether I have followed the right way.. can anybody please help me?

Thanks in advance.

1 Answer 1

0

Here's how I did it:

private int GetColumnIndex(GridView gv, string columnName, int columnCount)
{
    for (int i = 0; i < gv.Columns.Count; i++)
        if (gv.Columns[i].HeaderText == columnName)
            return i - columnCount + 1;
    throw new Exception("no such column '" + columnName + "'");
}

It works in my codebase.

1
  • Senior the thing is that am not getting the dgrDynamic.Columns.Count as 0.. why it is so.. and i think thats why foreach loop is not working Properly Mar 6, 2012 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.