473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Jul 24 '06 #1
6 7866
axas
32
Hello my friend. After having add the edit command, do this steps:

1.) make this method for binding
void BindGrid()
{
sqlDataAdapter1.Fill( dataSet1 );
DataGrid1.DataSource = dataSet1;
DataGrid1.DataKeyField = "xxxID";
DataGrid1.DataBind();
}

2.) handle this event

private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}

3.) handle this event

private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
}

4.) and this event...

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.
DataGridCommandEventArgs 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.Parameters["@field1"].Value= txtField1.Text;
sqlCommand1.Parameters["@field2"].Value = txtField2.Text;
sqlCommand1.Parameters["@xxxID"].Value = DataGrid1.DataKeys[ e.Item.ItemIndex ];

// Execute SqlCommand
sqlConnection1.Open();
sqlCommand1.ExecuteNonQuery();

// Deselect Row for Editing
DataGrid1.EditItemIndex = -1;
BindGrid();
sqlConnection1.Close();
}
Jul 24 '06 #2
axas
32
I have made a wrong...

Instead of txtProductName and txtUnitPrice e.g., write txtField1 and txtField2, to not be confused...
Jul 24 '06 #3
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.
Jul 24 '06 #4
axas
32
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?
Jul 24 '06 #5
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
Jul 24 '06 #6
axas
32
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 OleDbDataAdapter("SELECT * FROM [Sheet1$]",
ConfigurationSettings.AppSettings["Excel_0115_ConnectString"]);

// Create the insert command.
String insertSql = "INSERT INTO [Sheet1$] " +
"(CategoryID, CategoryName, Description) " +
"VALUES (?, ?, ?)";
da.InsertCommand =
new OleDbCommand(insertSql, da.SelectCommand.Connection);
da.InsertCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0,
"CategoryID");
da.InsertCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15,
"CategoryName");
da.InsertCommand.Parameters.Add("@Description", OleDbType.VarChar, 100,
"Description");

// Create the update command.
String updateSql = "UPDATE [Sheet1$] " +
"SET CategoryName=?, Description=? " +
"WHERE CategoryID=?";
da.UpdateCommand =
new OleDbCommand(updateSql, da.SelectCommand.Connection);
da.UpdateCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15,
"CategoryName");
da.UpdateCommand.Parameters.Add("@Description", OleDbType.VarChar, 100,
"Description");
da.UpdateCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 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.DataSource = dt.DefaultView;
Jul 26 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: alton.goerby | last post by:
Hello, im using a data bound numeric up down control, the problem is the binding updates the data source only if the numeric up down control looses focus, but i want it the binding to update...
1
by: MrMike | last post by:
Hi. My application has dozens of datagrids but for some reason an exception occurs when one of them is updated. When a user edits a datagrid row and then clicks 'Update' the following exception...
0
by: David Linsin | last post by:
I created a simple test case to reproduce the problem (also check Bug #15500): import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
4
by: geof | last post by:
Hi ! this is my first post where is my error code in line 15 ($sql ) <html> <head><title> update MySQL Data </title></head> <body bgcolor="#FFFFFF"> <?
1
by: johnlim20088 | last post by:
Hi All, I does have a question about export datagrid data to csv. Well, I know the common solution is 1) select the data again and put in the datatable, then export to csv. BUT I don't want...
4
MitchR
by: MitchR | last post by:
Good Morning; I have an issue that I need to guidance to resolve. I have a table called return_tbl with about 25k records. I have 23 Fields in this table. I am looking to update 3 of these 23...
11
by: cooperkuo | last post by:
Dear all, I have a question about ADO in the subform. I know how to use ADO to insert/update/select data into the sigin form, but wehn I try to do it in the form with subform((Datasheet). I don't...
3
by: vbdude | last post by:
Hi, I want help with my problem. How can i export datagrid data that comes from a ms sql server 2005 and will be going to ms excel.. Vbdude... P.S. Please Help me!!!
6
by: slinky | last post by:
I found the following code to transfer datagrid data to an Excel file. Is this written in C#?... I'm a vb.netter. I'm just not sure where to place the code to experiment on it. Should I place it in...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.