SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

DataGrid troubles

Last post 09-23-2008, 19:31 by mjohl.inox. 3 replies.
Sort Posts: Previous Next
  •  08-23-2008, 18:03 14270

    DataGrid troubles

    Hi All,

    Datagrid on a webpage. The datasource is a dataset created using the DataAdapter's wizard. The DataMember is the table in the dataset. I've got the grid.DataBind() in the Page_Load(), but when the page displays, there is no data displayed in the grid, although the column headings are there that I created using grid's Property Builder.

    Then: any tips on how to then EDIT and/or DELETE a row from the grid (must I use the grid's UpdateCommand, EditCommand, DeleteCommand, CancelCommand for this, or is there another way?)

    Another Q on the side: What is the DataKeyField of the grid used for?

    I know this is a lot asked in one post, but try and answer them all, PLEASE Confused Confused

    Thanx

  •  08-24-2008, 16:17 14276 in reply to 14270

    Re: DataGrid troubles

    Hi All,

    My datagrid is now displaying what is necessary, but the updating is still a problem. Here is my code:

    private void gridItems_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {

    Image1.Visible = false;

    TextBox txtName = new TextBox();

    TextBox txtImage = new TextBox();

    TextBox txtPrice = new TextBox();

    TextBox txtDescription = new TextBox();

    txtName = (TextBox)e.Item.Cells[1].Controls[0];

    txtImage = (TextBox)e.Item.Cells[2].Controls[0];

    txtPrice = (TextBox)e.Item.Cells[3].Controls[0];

    txtDescription = (TextBox)e.Item.Cells[4].Controls[0];

     

    string strSQL = "update [Items] set [Item Name] = '" + txtName.Text + "', [Image Name] = '" + txtImage.Text + "', [Unit Price] = " + txtPrice.Text + ", [Unit Description] = '" + txtDescription.Text + "' where [Item ID] = '" + e.Item.Cells[0].Text + "'";

    SqlCommand comm = new SqlCommand(strSQL,conn);

    try

    {

    conn.Open();

    comm.ExecuteNonQuery();

    lblAdminItemMessage.Text += " Item successfully updated.";

    }

    catch (Exception ex)

    {

    Response.Write(ex.Message);

    }

    finally

    {

    conn.Close();

    gridItems.EditItemIndex = -1;

    BindData();

    }

    }

     

    When I step (debug) through the code, the values new updated values that must be updated are not put into the new textboxes.

    I got this procedure of doing from the www.

    PLease advise.

    Thanx

  •  09-22-2008, 11:08 14922 in reply to 14276

    Re: DataGrid troubles

    There is a problem with the way you are trying to get the controls that containt the data required for performing the update. You do not need to create instances of the textbox using an initializer (= new TextBox). Moreover you need to use Findcontrol instead of assigning a textbox to the value of a cell. Well, to cut the story short, the code should be along these lines- try this:

    TextBox txtName ;

    TextBox txtImage ;

    TextBox txtPrice ;

    TextBox txtDescription ;

    txtName = (TextBox)e.Item.Cells[1].FindCotrol("nameOfTheTextBoxtoFind");

    txtImage = (TextBox)e.Item.Cells[2].FindCotrol("nameOfTheTextBoxtoFind");

    txtPrice = (TextBox)e.Item.Cells[3].FindCotrol("nameOfTheTextBoxtoFind");;

    txtDescription = (TextBox)e.Item.Cells[4].FindCotrol("nameOfTheTextBoxtoFind");

     

    and  remember to check if cell index is zero-based and that you also have to check if the control was found before you attempt to get the value. Use code similar to this:

    string name=string.empty; 

    if(txtName !=null)

    { name= txtName.Text;}

     

     

    Happy Coding


     

     


    There is no competition, its just me.
  •  09-23-2008, 19:31 14960 in reply to 14922

    Re: DataGrid troubles

    I had a similar problem with a datagrid on our intranet site at work.

    The problem was that when the page reloaded, I never checked for postback and default load data was re-used the whole time. Once I checked for post back the problem was solved.

    I used:
    private void Page_Load()
    {
       if (IsPostBack)
       {
          load_Update();
       }
       else (!IsPostBack)
       {
          load_Initial();
       }
    }

     


    What not...if not.

    Software is never finished and can always be improved.
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems