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

Gridview Update - cannot retrieve new values

It doesn't look like its possible to retrieve new values if there is a gridview without datasource.

Can someone please translate this for me from C# to VB. I can't get this to work and I was hoping this would solve my problem trying to update a gridview without datasource.

Here's the code

((TextBiox)grid.Rows[e.RowIndex].Cells[0].Controls[0]).Text

Thanks for any help on this.

~Haleigh
Dec 21 '06 #1
9 15087
bplacker
121 100+
It doesn't look like its possible to retrieve new values if there is a gridview without datasource. However, I seen one solution on http://forums.asp.net/1012665/ShowPost.aspx.

Can someone please translate this for me from C# to VB. I can't get this to work and I was hoping this would solve my problem trying to update a gridview without datasource.

Here's the code

((TextBiox)grid.Rows[e.RowIndex].Cells[0].Controls[0]).Text

Thanks for any help on this.

~Haleigh
TextBox.Text = grid.selectedRows(0).Cells(0).Value.ToString
Dec 21 '06 #2
TextBox.Text = grid.selectedRows(0).Cells(0).Value.ToString
Thank You!
Dec 21 '06 #3
I'm having the same issue that the original poster is having, but this trick isn't working for me. The only thing I'm doing differently (I think) is that I'm creating my grid dynamically. I've tried every method I can find to get the new value for a control, and it's continually coming back as the old method. Is this a limitation of creating the grid dynamically? I'm doing this in C#.
Apr 24 '08 #4
Frinavale
9,735 Expert Mod 8TB
I'm having the same issue that the original poster is having, but this trick isn't working for me. The only thing I'm doing differently (I think) is that I'm creating my grid dynamically. I've tried every method I can find to get the new value for a control, and it's continually coming back as the old method. Is this a limitation of creating the grid dynamically? I'm doing this in C#.
Could you explain more about your problem?
I'm not clear on what you are trying to do. Are you trying to update a DataSet through a GridView? Are you trying to update a GridView based on a DataSet?

When you are explaining your problem please post any code snippets that will help us understand what you are doing. Please remember to use [code] tags when you post code.

-Frinny
Apr 24 '08 #5
Well, I've got my data just fine. I just need the data, and I'll then run a stored procedure to update the values, THEN re-query it. Here's most of the code-behind. I'm still learning .NET, so it's a bit ungainly. I plan on parsing out a good bit of it to other components. It wouldn't let me post the whole thing.

Expand|Select|Wrap|Line Numbers
  1.     protected void InitDataTable(DataTable dt)
  2.     {
  3.         DataColumn dcol = new DataColumn("Doff Number", typeof(System.Int64));
  4.         dt.Columns.Add(dcol);
  5.         dcol = new DataColumn("Length", typeof(System.Int64));
  6.         dt.Columns.Add(dcol);
  7.         dcol = new DataColumn("Doff Length", typeof(System.Int64));
  8.         dt.Columns.Add(dcol);
  9.         dcol = new DataColumn("Start Time", typeof(System.DateTime));
  10.         dt.Columns.Add(dcol);
  11.         dcol = new DataColumn("End Time", typeof(System.DateTime));
  12.         dt.Columns.Add(dcol);
  13.         dcol = new DataColumn("WeavingDoffId", typeof(System.Guid));
  14.         dt.Columns.Add(dcol);
  15.     }
  16.  
  17.     protected void LoadDataTable(DataTable dt)
  18.     {
  19.         CMetalsWeavingDoffs cmwd = new CMetalsWeavingDoffs();
  20.  
  21.         LoadMetalsWeavingDoffs(cmwd);
  22.         foreach (SMetalsWeavingDoffs smwd in cmwd)
  23.         {
  24.             DataRow drow = dt.NewRow();
  25.             drow["Doff Number"] = smwd.WeavingDoffNumber;
  26.             drow["Length"] = smwd.Length;
  27.             drow["Doff Length"] = GetDoffLength(smwd.WorkOrderActualId);
  28.             drow["Start Time"] = smwd.StartTime;
  29.             drow["End Time"] = smwd.EndTime;
  30.             drow["WeavingDoffId"] = smwd.WeavingDoffId;
  31.             dt.Rows.Add(drow);
  32.         }
  33.     }
  34.  
  35.     protected void InitGrid(DataTable dt)
  36.     {
  37.         ButtonField confirmField = new ButtonField();
  38.         ButtonField scrapField = new ButtonField();
  39.         CommandField editField = new CommandField();
  40.  
  41.         if (!Page.IsPostBack)
  42.         {
  43.             foreach (DataColumn col in dt.Columns)
  44.             {
  45.                 BoundField bfield = new BoundField();
  46.                 bfield.DataField = col.ColumnName;
  47.                 bfield.HeaderText = col.ColumnName;
  48.                 if (bfield.DataField != "WeavingDoffId")
  49.                     grdDynamic.Columns.Add(bfield);
  50.                 if (bfield.DataField != "Length")
  51.                     bfield.ReadOnly = true;
  52.             }
  53.             confirmField.HeaderText = "Confirm";
  54.             confirmField.Text = "Confirm";
  55.             confirmField.CommandName = "Confirm";
  56.             confirmField.ButtonType = ButtonType.Button;
  57.             grdDynamic.Columns.Add(confirmField);
  58.             scrapField.HeaderText = "Scrap";
  59.             scrapField.Text = "Scrap";
  60.             scrapField.CommandName = "Scrap";
  61.             scrapField.ButtonType = ButtonType.Button;
  62.             grdDynamic.Columns.Add(scrapField);
  63.             editField.HeaderText = "Edit";
  64.             editField.EditText = "Edit Length";
  65.             editField.UpdateText = "Update";
  66.             editField.CancelText = "Cancel";
  67.             editField.ButtonType = ButtonType.Button;
  68.             editField.ShowEditButton = true;
  69.             editField.CausesValidation = false;
  70.             grdDynamic.Columns.Add(editField);
  71.  
  72.         }
  73.         grdDynamic.DataSource = dt;
  74.         grdDynamic.DataBind();
  75.     }
  76.  
  77.     protected void CreateGrid()
  78.     {
  79.         string[] dataKeyNames = new string[2];
  80.  
  81.         grdDynamic.AutoGenerateColumns = false;
  82.         grdDynamic.SkinID = "MainGridView";
  83.         grdDynamic.RowCommand += new GridViewCommandEventHandler(grdDynamic_RowCommand);
  84.         grdDynamic.RowEditing += new GridViewEditEventHandler(grdDynamic_RowEditing);
  85.         grdDynamic.RowCancelingEdit += new GridViewCancelEditEventHandler(grdDynamic_RowCancelEditing);
  86.         grdDynamic.RowUpdating += new GridViewUpdateEventHandler(grdDynamic_RowUpdating);
  87.         dataKeyNames[0] = "WeavingDoffId";
  88.         dataKeyNames[1] = "Length";
  89.         if (!Page.IsPostBack)
  90.             grdDynamic.DataKeyNames = dataKeyNames;
  91.         Page.Form.Controls.Add(grdDynamic);
  92.     }
  93.  
  94.     protected void BindGrid()
  95.     {
  96.         DataTable dt = new DataTable();
  97.  
  98.         try
  99.         {
  100.             InitDataTable(dt);
  101.             LoadDataTable(dt);
  102.             InitGrid(dt);
  103.         }
  104.         catch(Exception ex)
  105.         {
  106.             lblError.Text += ex.Message;
  107.         }
  108.     }
  109.  
  110.     protected void Page_Load(object sender, EventArgs e)
  111.     {
  112.         Response.Expires = -1600;
  113.         lblError.Text = string.Empty;
  114.         CreateGrid();
  115.         if (!Page.IsPostBack)
  116.             BindGrid();
  117.     }
  118.  
  119.     protected void grdDynamic_RowCommand(object sender, GridViewCommandEventArgs e)
  120.     {
  121.         try
  122.         {
  123.             if (e.CommandName == "Confirm")
  124.             {
  125.                 ConfirmDoff(grdDynamic.DataKeys[Convert.ToInt32(e.CommandArgument)].Values["WeavingDoffId"].ToString(), Convert.ToInt32(grdDynamic.DataKeys[Convert.ToInt32(e.CommandArgument)].Values["Length"]));
  126.                 ConfirmDoffOld(grdDynamic.DataKeys[Convert.ToInt32(e.CommandArgument)].Values["WeavingDoffId"].ToString());
  127.             }
  128.             if (e.CommandName == "Scrap")
  129.                 ScrapDoff(grdDynamic.DataKeys[Convert.ToInt32(e.CommandArgument)].Values["WeavingDoffId"].ToString(), Convert.ToInt32(grdDynamic.DataKeys[Convert.ToInt32(e.CommandArgument)].Values["Length"]));
  130.             BindGrid();
  131.         }
  132.         catch (Exception ex)
  133.         {
  134.             lblError.Text = ex.Message;
  135.         }
  136.     }
  137.  
  138.     protected void grdDynamic_RowEditing(object sender, GridViewEditEventArgs e)
  139.     {
  140.         grdDynamic.EditIndex = e.NewEditIndex;
  141.         BindGrid();
  142.     }
  143.  
  144.     protected void grdDynamic_RowCancelEditing(object sender, GridViewCancelEditEventArgs e)
  145.     {
  146.         grdDynamic.EditIndex = -1;
  147.         BindGrid();
  148.     }
  149.  
  150.     protected void grdDynamic_RowUpdating(object sender, GridViewUpdateEventArgs e)
  151.     {
  152.         Guid weavingDoffId = new Guid(grdDynamic.DataKeys[Convert.ToInt32(e.RowIndex)].Values["WeavingDoffId"].ToString());
  153.         //DataControlFieldCell cell = new DataControlFieldCell(grdDynamic.Rows[e.RowIndex].Cells[0]);
  154.  
  155.         grdDynamic.EditIndex = -1;
  156.         lblError.Text = "I do the update on " + e.OldValues.Count + "!  Huzzah!";
  157.         //DataControlFieldCell cell = grdDynamic.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
  158.         //grdDynamic.Columns[1].ExtractValuesFromCell(e.OldValues, cell, DataControlRowState.Edit, true);
  159.         //lblError.Text = e.OldValues[0].ToString();
  160.         //lblError.Text = grdDynamic.Rows[0].Cells[0].Controls[0].ToString();
  161.         //TextBox newBox = (TextBox)grdDynamic.Rows[0].Cells[1].Controls[0];
  162.         //lblError.Text = newBox.Text;
  163.         try
  164.         {
  165.             //UpdateLength(weavingDoffId, Convert.ToInt32(grdDynamic.DataKeys[Convert.ToInt32(e.RowIndex)].Values["Length"]));
  166.         }
  167.         catch (Exception ex)
  168.         {
  169.             lblError.Text = ex.Message;
  170.         }
  171.         BindGrid();
  172.     }
  173.  
  174.     protected void btnRefresh_Click(object sender, EventArgs e)
  175.     {
  176.         BindGrid();
  177.     }
  178. }
  179.  
The HTML is very sparse:

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Metals Doff Confirmation</title>
  8.     <link href="PMDStyles.css" rel="stylesheet" type="text/css" />
  9. </head>
  10. <body>
  11.     <form id="form1" runat="server">
  12.     <div style="text-align: center;">
  13.         Click here to&nbsp;
  14.         <asp:Button ID="btnRefresh" runat="server" Text="Refresh" 
  15.             onclick="btnRefresh_Click" />
  16.         &nbsp; the page.
  17.     </div>
  18.     <hr />
  19.     <div style="text-align: center;">
  20.     <div style="text-align: center;">
  21.         <asp:Label ID="lblError" runat="server"></asp:Label>
  22.     </div>
  23.     </form>
  24. </body>
  25. </html>
  26.  
The main problem I'm having is with the grdDynamic_RowUpdating subroutine. I can get the current value all I want to. The commented lines are my various attempts to read the NEW data that the user inputs. None of them will retrieve anything but the old data.

The Confirm and Scrap commands all work fine, as do my refresh and display routines. The ONLY thing that won't return is the new data.
Apr 24 '08 #6
Frinavale
9,735 Expert Mod 8TB
Well, I've got my data just fine. I just need the data, and I'll then run a stored procedure to update the values, THEN re-query it. Here's most of the code-behind. I'm still learning .NET, so it's a bit ungainly. I plan on parsing out a good bit of it to other components. It wouldn't let me post the whole thing.


The main problem I'm having is with the grdDynamic_RowUpdating subroutine. I can get the current value all I want to. The commented lines are my various attempts to read the NEW data that the user inputs. None of them will retrieve anything but the old data.

The Confirm and Scrap commands all work fine, as do my refresh and display routines. The ONLY thing that won't return is the new data.
In the future just post the portion of your code that's causing problems.
Are you doing a DataBind in your PageLoad event?
When you do this your data is bound to your grid view and your newly entered information is lost...it's quite a common occurrence to GridView update problems.

-Frinny
Apr 25 '08 #7
I do the data bind only on the first page load. From then on, I run the data bind after the update code. Can I not run them in that order?
Apr 28 '08 #8
apaluk
1
I had exactly the same problem and finally with my colleague's help we found the mistake.
I was doing databinding in my Page_Load event and when I clicked "Update" the PostBack was done, so was the Page_Load event fired. GridView was filled with the old values again, so in myGrid.RowUpdating event I couldn't retrieve new values.
Jul 25 '08 #9
Thanks a lot Ur Reply helps me too
Jan 24 '11 #10

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

Similar topics

4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
1
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an...
0
by: Mike P | last post by:
How do you get access to the values you have entered under edit mode in a gridview? I only seem to be able to access the values prior to them being edited. protected void...
2
by: Greg | last post by:
Hello, I am trying to bind a GridView to a custom object I have created. First, here is what I'm trying to do: I have a wizard for adding/editing Users. When the wizard begins, a User...
3
by: pvong | last post by:
VB.NET How do you change a gridview from Update mode to normal mode? I'm usually dealing for Formviews and there is a ChangeMode option but I don't see one for Gridviews.
4
by: tim.cavins | last post by:
I have a GridView populated by an ObjectDataSource. I am having issues passing the parameters to the objectdatasource. I have verified that the method is being called but none of the parameters...
1
by: Joe Blauth | last post by:
Hi all, I was running into a problem with a gridview under ASP.net. What I am trying to do is binding the DataSource dynamically in a way that enables me to edit the rows in the Gridview. This...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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.