DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Datagrid Textbox Help.......PLEASE!!!!!

  1. #1
    Join Date
    Jan 2007
    Posts
    2

    Datagrid Textbox Help.......PLEASE!!!!!

    I have a fully edit-able datagrid that will eventually update a sql database.

    The problem I am having with the code below, is when it sends the paramaters to the proc, they are all empty strings, or 0.0 for the decimal types.

    In the VB code, you will notice two different lines for the depositType as String....one is commented out......this is because I have tried both of these, and with the first one that is not commented out, I get an exception of object is not set to an instance of an object, and with all the others, it does not pass the values to the variables. When I step through it, they are all empty, or 0.0.

    Any help would be wonderful, Thank you

    MARK UP::::::::

    <

    asp: DataGrid ID="gridEdit" runat="server" style="text-align:center; font-size:.8em" AutoGenerateColumns="False" TabIndex="9" CellPadding="4" GridLines="None" ForeColor="#333333">

    <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />

    <EditItemStyle BackColor="#999999" />

    <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

    <AlternatingItemStyle BackColor="White" ForeColor="#284775" />

    <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />

    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

    <Columns>

    <asp:BoundColumn HeaderText="Category ID" DataField="CatID" Visible="false"></asp:BoundColumn>



    <asp:TemplateColumn HeaderText="Type of Deposit">

    <ItemTemplate>

    <asp:TextBox id="txtDepositType" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.depositType2") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Minimum Amount to Open Account">

    <ItemTemplate>

    <asp:TextBox id="txtMinToOpen" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.minToOpen2") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Maturity">

    <ItemTemplate>

    <asp:TextBox id="txtMaturity" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.maturity2") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Minimum Balance to Earn APY">

    <ItemTemplate>

    <asp:TextBox id="txtMinBalanceToEarn" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.minBalanceToEarn2") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Interest Rate %">

    <ItemTemplate>

    <asp:TextBox id="txtInterestRate" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.interestRate2") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Annual Percentage Yield % (APY)">

    <ItemTemplate>

    <asp:TextBox id="txtAPY" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.apy") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Compounding Frequency">

    <ItemTemplate>

    <asp:TextBox id="txtCompFreq" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.compFreq2") %>'>

    </asp:TextBox>

    </ItemTemplate>

    </asp:TemplateColumn>



    <asp:TemplateColumn HeaderText="Rate ID">

    <ItemTemplate>

    <asp:label id="lblRateID" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.rateID2") %>'>

    </asp:label>

    </ItemTemplate>

    </asp:TemplateColumn>







    </Columns>

    </asp: DataGrid>

    <asp:Label ID="lblError" runat="server" Height="38px" Width="1037px"></asp:Label>

    <asp:Button ID="btnSaveAll" runat="server" Text="Save All Changes" />


    VB CODE:::::::::::::::::::::::::::::::::::

    Protected

    Sub btnSaveAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAll.Click

    Try

    'Dim gridEdit2 As New DataGrid

    'gridEdit2 = gridEdit



    Dim item4 As DataGridItem

    Dim myCommitConnection As Data.SqlClient.SqlConnection

    Dim sqlProcedure As String
    sqlProcedure =

    "details_update"
    myCommitConnection =

    New Data.SqlClient.SqlConnection("server=SNSQLDEV01;" & "database=StifelBank;Trusted_Connection=Yes")

    Dim cmd As New Data.SqlClient.SqlCommand(sqlProcedure, myCommitConnection)
    cmd.CommandType = System.Data.CommandType.StoredProcedure


    For Each item4 In gridEdit.Items

    Dim depositType As String = CType(gridEdit.FindControl("txtDepositType"), TextBox).Text

    ' Dim depositType As String = Convert.ToString(gridEdit.FindControl("txtDepositType"))

    Dim minToOpen As String = Convert.ToString(gridEdit.FindControl("txtMinToOpen"))

    Dim maturity As String = Convert.ToString(gridEdit.FindControl("txtMaturity"))

    Dim balanceToEarn As String = Convert.ToString(gridEdit.FindControl("txtMinBalanceToEarn"))

    Dim interestRate As Double = Convert.ToDouble(gridEdit.FindControl("txtInterestRate"))

    Dim apy As Double = Convert.ToDouble(gridEdit.FindControl("txtAPY"))

    Dim compFreq As String = Convert.ToString(gridEdit.FindControl("txtCompFreq"))

    Dim rateID As Integer = Convert.ToInt32(gridEdit.FindControl("lblRateID"))
    cmd.Parameters.Add(

    "@depositType", SqlDbType.VarChar).Value = depositType
    cmd.Parameters.Add(

    "@minToOpen", SqlDbType.VarChar).Value = minToOpen
    cmd.Parameters.Add(

    "@maturity", SqlDbType.VarChar).Value = maturity
    cmd.Parameters.Add(

    "@minBalanceToEarn", SqlDbType.VarChar).Value = balanceToEarn
    cmd.Parameters.Add(

    "@interestRate", SqlDbType.Decimal).Value = interestRate
    cmd.Parameters.Add(

    "@apy", SqlDbType.Decimal).Value = apy
    cmd.Parameters.Add(

    "@compFreq", SqlDbType.VarChar).Value = compFreq
    cmd.Parameters.Add(

    "@rateID", SqlDbType.Int).Value = rateID

    Next
    myCommitConnection.Open()

    cmd.ExecuteNonQuery()

    myCommitConnection.Close()


    Catch ex As Exception

    'lblerror.text = "Error Processing Save: " ex

    Me.lblError.Text = GetExceptionMessage(ex)
    lblError.ForeColor = System.Drawing.Color.Red


    End Try

    End Sub
    End

    Class

  2. #2
    Join Date
    Apr 2009
    Posts
    1

    This is C#, but easily translatable in to VB or...

    In some cases, folks like to dynamically populate their DataGrids and have the columns autogenerate. I do this most of the time, and also had the need to adjust column width. I've seen ONE post so far in all my searching that mentioned how to do it in code, but it did not work. Below, you will see a simple function that I wrote in C# to address the issue of customizing datagrid child controls.



    As you can see, this is currently only set up to work with DataGrid Text Boxes, but can easily be update to handle other kinds of DataGrid child controls.


    Feel free to use this code as is or modify to do what you want it to. Here is the sample code also, to use the function....Oh, BTW, this function is best called within the DataGrid Edit Command Event Handler.


    Example Call Statement:

    AdjustDataGridTextBoxWidth(<Name of DataGrid Here>, "TextBox", 110, 4, TextBoxMode.MultiLine, 8);


    The Function Code:

    private void AdjustDataGridTextBoxWidth(DataGrid GridName, string SubControlType, Unit Width, int RowsCount, TextBoxMode TBMode, FontUnit FontSize)

    {

    if (SubControlType == "TextBox")

    {

    for (int i = 0; i < GridName.Items.Count; i++)

    {

    for (int o = 1; o < GridName.Items[i].Cells.Count; o++)

    {

    for (int x = 0; x < GridName.Items[i].Cells[o].Controls.Count; x++)

    {

    if (GridName.Items[i].Cells[o].Controls[x].GetType().ToString() == "System.Web.UI.WebControls.TextBox")

    {

    TextBox GridTextBox = (TextBox)GridName.Items[i].Cells[o].Controls[x];

    GridTextBox.Width = Width;

    GridTextBox.Rows = RowsCount;

    GridTextBox.TextMode = TBMode;

    GridTextBox.Font.Size = FontSize;

    }

    }

    }

    }

    }

    }

  3. #3
    Join Date
    Apr 2009
    Posts
    1
    Hi,

    As you can see, this is currently only set up to work with DataGrid Text Boxes, but can easily be update to handle other kinds of DataGrid child controls.


    Feel free to use this code as is or modify to do what you want it to. Here is the sample code also, to use the function....Oh, BTW, this function is best called within the DataGrid Edit Command Event Handler.

    Thanks.




    *links removed*

Similar Threads

  1. Datagrid with multiple datasources
    By srinivasc_it in forum ASP.NET
    Replies: 0
    Last Post: 12-11-2006, 10:25 AM
  2. Replies: 0
    Last Post: 09-02-2005, 07:28 PM
  3. how to control textbox using datagrid
    By whygh in forum VB Classic
    Replies: 1
    Last Post: 06-07-2005, 06:27 PM
  4. update on datagrid not firing
    By tabwebdesign in forum ASP.NET
    Replies: 1
    Last Post: 05-20-2005, 12:23 PM
  5. Threading and the .NET Datagrid
    By McCleud in forum .NET
    Replies: 2
    Last Post: 08-01-2002, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center