Adding rows to a datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Deepa

    #1

    Adding rows to a datagrid

    Hi
    I am doing a web application in which I have a datagrid with 3 columns. I am
    populating the 1st column(label) and the 3rd column(dropdown list) from the
    database. Before the 1st column I added an EditCommandColu mn which displays
    the Edit button for me. But when I click the Edit button for a particular
    row, it sure displays a textbox for editing but I can't see the previous text
    anymore. The same with the 3rd column's dropdown,,it shows an empty dropdown
    list. I am calling the method for re-binding the grid as

    protected void dgDetails_Edit( object sender, DataGridCommand EventArgs e)
    {
    dgDetails.EditI temIndex = e.Item.ItemInde x;
    BindDataGrid();
    }

    and this is my BindDataGrid() method definition

    private void BindDataGrid()
    {
    //DataTable dt = new DataTable();
    connObj = this.OpenConnec tion();
    strSql = "Select Remarks from HDSurveyMaster" ;
    dsRemarks = SqlHelper.Execu teDataset(conn, CommandType.Tex t,strSql);

    strSql = "Select DisplayOrder from HDSurveyMaster" ;
    dsOrder = SqlHelper.Execu teDataset(conn, CommandType.Tex t,strSql);
    object order=null;

    try
    {
    DataColumn cRemarks = new DataColumn();
    cRemarks.Captio n = "Remarks";
    cRemarks.Column Name = "Remarks";
    dt.Columns.Add( cRemarks);

    DataColumn cOrder = new DataColumn();
    cOrder.Caption = "Order";
    cOrder.ColumnNa me = "Order";
    dt.Columns.Add( cOrder);

    for (int i=0;i<dsRemarks .Tables[0].Rows.Count;i++ )
    {
    DataRow dr;
    dr = dt.NewRow();
    dr["Remarks"] = dsRemarks.Table s[0].Rows[i]["Remarks"];

    for(int j=0; j< dsOrder.Tables[0].Rows.Count; j++)
    {
    order = dsOrder.Tables[0].Rows[j]["DisplayOrd er"];
    }
    dr["Order"] = order;
    dt.Rows.Add(dr) ;
    }
    ds.Clear();
    ds.Tables.Add(d t);
    dgDetails.DataS ource = ds.Tables[0].DefaultView;
    dgDetails.DataB ind();
    }

    catch(AFExcepti on af)
    {
    throw af;
    }
    return;
    }
    Am not sure where am going wrong.

    That aside, I also need to write the code if the user wants to add a new
    row. How do I do that?

    Can anyone help me with it?

    Thanks
Working...