Pass textbox value to a column in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SK2010
    New Member
    • Jul 2010
    • 6

    Pass textbox value to a column in gridview

    In my gridview, I have 6 columns. In the 6th column I want to display a value from textbox from same form. The value should be displayed on the row next to last row of rest of the columns. So my last column (i.e.) 6th column will just have 1 row irrespective of number of rows in rest of the columns.

    How do I achieve this? Please help me. I have been trying a lott but couldnt figure out.
  • djpaul
    New Member
    • Oct 2006
    • 137

    #2
    Hey,
    Try this:
    Code:
    DataGridView1.Item("ColumnName", 0).Value = TextBox1.Text
    Greetz,
    Paul

    Comment

    • SK2010
      New Member
      • Jul 2010
      • 6

      #3
      Thanks Paul for helping me solve my problem.

      I tried doing as you said, but I got an error message "System.Web.UI. WebControls.Gri dView does not contain a definition for Item"

      I have put this line of code in my databound event. Am I doing anything wrong?

      Here is my databound event code:
      Code:
      GridView gvr = sender as GridView;
                         
              if (gvr != null)
             {
                     GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
      
                      TableCell cell = new TableCell();
                      cell.ColumnSpan = 1;
                      cell.HorizontalAlign = HorizontalAlign.Center;
                      cell.Text = "Quantity";
                      row.Cells.Add(cell);
      
                      cell = new TableCell();
                      cell.ColumnSpan = 4;
                      cell.HorizontalAlign = HorizontalAlign.Center;
                      cell.Text = "Description";                 
                      row.Cells.Add(cell);
                      //grdItems.Rows[2].Cells[3].Text = "sample Text"; 
                      
      
                      cell = new TableCell();
                      cell.ColumnSpan = 1;
                      cell.HorizontalAlign = HorizontalAlign.Center;
                      cell.Text = "Total Amount";
                      row.Cells.Add(cell);
                      
                      grdItems.Controls[0].Controls.AddAt(0, row);
                      grdItems.Item("Total Amount", 0).Value = txtAmtDue.Text;
                     
                  }

      Comment

      • djpaul
        New Member
        • Oct 2006
        • 137

        #4
        Oh sh...t. Sorry, it was on asp.net. I don't know much about that.
        Maybe you could try:

        Code:
        grdItems.Rows[1].Cells[3].Text = txtAmtDue.Text;
        Hope that will help you...
        Cheers!

        Comment

        • SK2010
          New Member
          • Jul 2010
          • 6

          #5
          djpaul,

          Thanks for helping me. I tried as you said...but it didnt work...

          I tried using template field and then bind the value but for some reason I dont see the value when I run the page.

          here is my sql query I used to bind the gridview:
          Select bi.*, b.cAmtDue AS cAmtDue FROM bod_items bi, bod_inv b WHERE bi.cInvNum = '" + Request.QuerySt ring["cInvNum"].ToString() + "' and bi.cInvNum = b.cInvNum

          Here is my aspx for grid:
          Code:
          <asp:GridView ID="grdItems"  runat="server" AllowSorting="True" AutoGenerateColumns="False"
                      BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
                      CellPadding="4" ForeColor="Black" GridLines="Vertical" width="670px" Font-Size="Smaller">
                      <Columns>
                      <asp:TemplateField HeaderText="ROWID" SortExpression="rowid" Visible="False">
                              <EditItemTemplate>
                                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("rowid") %>'></asp:TextBox>
                              </EditItemTemplate>
                              <ItemTemplate>
                                  <asp:Label ID="lblRowID" runat="server" Text='<%# Bind("rowid") %>'></asp:Label>
                              </ItemTemplate>
                              <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                              <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                          </asp:TemplateField>
                         ...
                               
                           
                <asp:TemplateField>   
                
                <HeaderTemplate>Total Amount</HeaderTemplate>   
                <FooterTemplate>   
                <table border="1" cellpadding="0" cellspacing="0" width="100%">   
                <ItemTemplate>
                   <asp:Label ID="lblRowID" runat="server" Text='<%# Eval("cAmtDue") %>'></asp:Label>
                </ItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("cAmtDue") %>'></asp:Label>
                </ItemTemplate>      
                </table>   
                </FooterTemplate>   
                </asp:TemplateField>   
               </Columns>
           ...
                  </asp:GridView>

          Comment

          Working...