473,378 Members | 1,544 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,378 software developers and data experts.

The different - In Gridview -Updating row

code: I have read a example :The use girdview without datasource

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView AutoGenerateColumns="false" ID="GridView1"
  2. runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit"
  3. OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
  4. <Columns>
  5. <asp:CommandField ShowEditButton="true" />
  6. <asp:BoundField HeaderText="ID" DataField="ID"
  7. ReadOnly="true" />
  8. <asp:TemplateField HeaderText="Name">
  9.     <ItemTemplate>
  10.         <asp:Label ID="lblName" runat="server" Text='<%#
  11.                                     Eval("Name") %>'>
  12.         </asp:Label>
  13.     </ItemTemplate>
  14. <EditItemTemplate>
  15.     <asp:TextBox ID="txtName" runat="server" Text='<%#
  16.                     Eval("Name") %>'></asp:TextBox>
  17. </EditItemTemplate>
  18. </asp:TemplateField>
  19. </Columns>
  20. </asp:GridView>
file .cs
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             if (!IsPostBack)
  4.             {
  5.                 BindGrid();
  6.             }
  7.         }
  8.  
  9.         private void BindGrid()
  10.         {
  11.             GridView1.DataSource = GetDataSource();
  12.             GridView1.DataBind();
  13.         }
  14.  
  15.         protected DataTable GetDataSource()
  16.         {
  17.             const string key = "MyDataSource";
  18.  
  19.             DataTable dt = Session[key] as DataTable;
  20.  
  21.             Session[key] = dt;
  22.             if (dt == null)
  23.             {
  24.                 dt = new DataTable();
  25.                 dt.Columns.Add("ID", typeof(int));
  26.                 dt.Columns.Add("Name", typeof(string));
  27.                 dt.Rows.Add(1, "first object");
  28.                 dt.Rows.Add(2, "second object");
  29.                 dt.Rows.Add(3, "three object");
  30.                 dt.Rows.Add(4, "four object");
  31.                 Session[key] = dt;
  32.             }
  33.  
  34.             return dt;
  35.         }
  36.  
  37.         protected void GridView1_RowEditing(object sender,
  38.         GridViewEditEventArgs e)
  39.         {
  40.             GridView1.EditIndex = e.NewEditIndex;
  41.             BindGrid();
  42.         }
  43.         protected void GridView1_RowCancelingEdit(object sender,
  44.         GridViewCancelEditEventArgs e)
  45.         {
  46.             GridView1.EditIndex = -1;
  47.             BindGrid();
  48.         }
  49.  
  50.         protected void GridView1_RowUpdating(object sender,
  51.         GridViewUpdateEventArgs e)
  52.         {
  53.             int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
  54.             TextBox txtName =
  55.             GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
  56.             string newname = txtName.Text;
  57.  
  58.             DataTable dt = GetDataSource();
  59.             DataRow[] rows = dt.Select("ID = " + id.ToString());
  60.             rows[0]["Name"] = newname;
  61.             GridView1.EditIndex = -1;
  62.             BindGrid();
  63.         }
  64.     }
the example have run

now I want change the function GetDatasource following

Expand|Select|Wrap|Line Numbers
  1. protected DataTable GetDataSource()
  2.         {
  3.         SqlConnection conn = new SqlConnection("server =Nguyenlh;uid =sa;pwd =1221984;database =Test");
  4.             conn.Open();
  5.             string sql = "select * from table1 ";
  6.             SqlDataAdapter data = new SqlDataAdapter(sql, conn);
  7.             DataSet ds = new DataSet();
  8.             data.Fill(ds, "table1");
  9.             DataTable table = ds.Tables["table1"];
  10.              return table;
  11.         }
but it not run :
It can binding but when I click update then it isn't update
who can help me
Nov 12 '07 #1
3 1561
kenobewan
4,871 Expert 4TB
Do you get any errors? What debugging have you tried?
Nov 12 '07 #2
Do you get any errors? What debugging have you tried?
When i click Update
it can't update
i try debug is add a function Update with 2 parameter
id and name :
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
TextBox txtName =
GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
Update(id, newname);
}
public void Update(int id, string name)
{
SqlConnection conn = new SqlConnection("server =Nguyenlh;uid=sa;pwd=1221984;database =Test");
conn.Open();


SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

cmd.CommandText = "Update table1 set Name =@Name where ID =@ID";

cmd.Parameters.Add(new SqlParameter("@Name",SqlDbType.NChar,20 ,"Name"));
cmd.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int,0, "ID"));

cmd.UpdatedRowSource = UpdateRowSource.None;
cmd.Parameters["@Name"].Value = name;
cmd.Parameters["@ID"].Value = id;
cmd.ExecuteNonQuery();
BindGrid();
conn.Close();
}
Nov 13 '07 #3
hi !

do u got answer to this problem......im also having that problem.....please help me to solve the problem..
Jan 28 '08 #4

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

Similar topics

6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
7
by: rockdale | last post by:
Hi, How do you guys deal with the different clinet resolution? For example, you have 800*600, 1024*768 and 1280*1024 on client side. We used to put everything into a table with fixed width (the...
1
by: ad | last post by:
I set a row of gridview different color when one value in the first column if greater the the value in the second column. How can I do?
3
by: tsedward | last post by:
I am creating a databound gridview. The last cell of each row can, and usually does, contain a lot of text. Is there any way that I could drop this last cell down into the next line on the...
2
by: amitsaxena1981 | last post by:
hi, I am using asp.net2.0 with vb.net , I have to create gridview user control and I want to drag and drop a record to a different position in the same Gridview using AJAX .Is it possible to...
2
by: bogdan | last post by:
Hi, Can a single GridView be 'connected' to DetailsView that renders itself differently based on the currently selected row? I have a GridView with rows that could be displayed in the same way...
0
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
How can I display a different control for my in my table, inside an ItemTemplate or EditTemplate, depending on the value of another column, the , which has values like 'STRING', 'INTEGER', ...
1
by: Jeff | last post by:
hi asp.net 2.0 I have a webpage with a gridview. Each row in the GridView contain info about a user (Membershipuser, standard asp.net 2.0 membership). One of the columns in the GridView is a...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.