How to update the Datagrid data.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • system123
    New Member
    • Jul 2006
    • 4

    #1

    How to update the Datagrid data.

    Hi All,

    With the Edit Linkbutton I am able to change the datagrid to Edit mode. After doing some modifictaions/editing the row I need to Update the Datagrid Display view with the Edited values.

    Please help me.


    Thanks in Advance.
  • axas
    New Member
    • Jul 2006
    • 32

    #2
    Hello my friend. After having add the edit command, do this steps:

    1.) make this method for binding
    void BindGrid()
    {
    sqlDataAdapter1 .Fill( dataSet1 );
    DataGrid1.DataS ource = dataSet1;
    DataGrid1.DataK eyField = "xxxID";
    DataGrid1.DataB ind();
    }

    2.) handle this event

    private void DataGrid1_EditC ommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    DataGrid1.EditI temIndex = e.Item.ItemInde x;
    BindGrid();
    }

    3.) handle this event

    private void DataGrid1_Cance lCommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    DataGrid1.EditI temIndex = -1;
    BindGrid();
    }

    4.) and this event...

    private void DataGrid1_Updat eCommand(object source, System.Web.UI.W ebControls.
    DataGridCommand EventArgs e)
    {
    // Retrieve TextBox Controls from DataGrid
    TextBox txtProductName = (TextBox)e.Item .Cells[2].Controls[0];
    TextBox txtUnitPrice = (TextBox)e.Item .Cells[3].Controls[0];

    // Assign Parameters to SqlCommand
    sqlCommand1.Par ameters["@field1"].Value= txtField1.Text;
    sqlCommand1.Par ameters["@field2"].Value = txtField2.Text;
    sqlCommand1.Par ameters["@xxxID"].Value = DataGrid1.DataK eys[ e.Item.ItemInde x ];

    // Execute SqlCommand
    sqlConnection1. Open();
    sqlCommand1.Exe cuteNonQuery();

    // Deselect Row for Editing
    DataGrid1.EditI temIndex = -1;
    BindGrid();
    sqlConnection1. Close();
    }

    Comment

    • axas
      New Member
      • Jul 2006
      • 32

      #3
      I have made a wrong...

      Instead of txtProductName and txtUnitPrice e.g., write txtField1 and txtField2, to not be confused...

      Comment

      • system123
        New Member
        • Jul 2006
        • 4

        #4
        Hi
        Thanq for your quickreply. Actually I am displaying the datagrid value by dynamically importing the excel file so the Fields of the Table are unknown.

        My requirement is to hold the chnages made in .aspx page in a dataset and update the datgrid accordingly.

        Please give some idea. I hope you have understood my problem.

        Comment

        • axas
          New Member
          • Jul 2006
          • 32

          #5
          Have you tried this code my friend?
          Is this ok?
          If you want to keep the changes in a dataset, clear the dataset first, and then use a DataAdapter to fill again the dataset. Is that you mean?

          Comment

          • system123
            New Member
            • Jul 2006
            • 4

            #6
            Hi Frd,
            Its working !!!
            hip hip hurry
            I made some changes after getting the textbox control text.thanx a million.

            One small request. I am importing excel file with file upload control but additionally i am getting blank rows into the datagrid. How to retrive only datafilled rows/columns of excel.
            I have given the SQLquery as
            Select * FROM [worksheetname$]

            Plz give ur valubale suggestions.

            Thanks in advance

            Comment

            • axas
              New Member
              • Jul 2006
              • 32

              #7
              There are three ways in which you can reference Excel workbook data within a SQL statement:

              Specify the worksheet name followed by a dollar sign to access the entire range used in the worksheet:

              SELECT * FROM [MySheet$]
              Specify a range explicitly using cells:

              SELECT * FROM [MySheet$A1:E5]
              Specify a range with a defined name, as shown in the solution:

              SELECT * FROM MyRange

              The following sample code is to show how you can have access in a Microsoft Excel Workbook:

              // Create the DataAdapter.
              da = new OleDbDataAdapte r("SELECT * FROM [Sheet1$]",
              ConfigurationSe ttings.AppSetti ngs["Excel_0115_Con nectString"]);

              // Create the insert command.
              String insertSql = "INSERT INTO [Sheet1$] " +
              "(CategoryI D, CategoryName, Description) " +
              "VALUES (?, ?, ?)";
              da.InsertComman d =
              new OleDbCommand(in sertSql, da.SelectComman d.Connection);
              da.InsertComman d.Parameters.Ad d("@CategoryID" , OleDbType.Integ er, 0,
              "CategoryID ");
              da.InsertComman d.Parameters.Ad d("@CategoryNam e", OleDbType.Char, 15,
              "CategoryName") ;
              da.InsertComman d.Parameters.Ad d("@Description ", OleDbType.VarCh ar, 100,
              "Descriptio n");

              // Create the update command.
              String updateSql = "UPDATE [Sheet1$] " +
              "SET CategoryName=?, Description=? " +
              "WHERE CategoryID=?";
              da.UpdateComman d =
              new OleDbCommand(up dateSql, da.SelectComman d.Connection);
              da.UpdateComman d.Parameters.Ad d("@CategoryNam e", OleDbType.Char, 15,
              "CategoryName") ;
              da.UpdateComman d.Parameters.Ad d("@Description ", OleDbType.VarCh ar, 100,
              "Descriptio n");
              da.UpdateComman d.Parameters.Ad d("@CategoryID" , OleDbType.Integ er, 0,
              "CategoryID ");

              // Fill the table from the Excel spreadsheet.
              dt = new DataTable( );
              da.Fill(dt);
              // Define the primary key.
              dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};

              // Records can only be inserted using this technique.
              dt.DefaultView. AllowDelete = false;
              dt.DefaultView. AllowEdit = true;
              dt.DefaultView. AllowNew = true;
              // Bind the default view of the table to the grid.
              dataGrid.DataSo urce = dt.DefaultView;

              Comment

              Working...