469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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 14885
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

Post your reply

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

Similar topics

4 posts views Thread by Nalaka | last post: by
7 posts views Thread by Ken | last post: by
1 post views Thread by Giovanni | last post: by
reply views Thread by Mike P | last post: by
2 posts views Thread by Greg | last post: by
3 posts views Thread by pvong | last post: by
4 posts views Thread by tim.cavins | last post: by
1 post views Thread by Joe Blauth | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.