locked
Gridview RRS feed

  • Question

  • User66371569 posted
     <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
    
            <Columns>
    
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
    
            <asp:TemplateField HeaderText="Header 1">
    
                <ItemTemplate>
    
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
                </ItemTemplate>
    
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Header 2">
    
                <ItemTemplate>
    
                   
                      <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                </ItemTemplate>
    
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Header 3">
    
                <ItemTemplate>
    
                     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    
                </ItemTemplate>
    
                <FooterStyle HorizontalAlign="Right" />
    
                <FooterTemplate>
    
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
    
                </FooterTemplate>
    
            </asp:TemplateField>
    
            </Columns>
    
    </asp:gridview>


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                SetInitialRow()
            End If
    
        End Sub
    
        
        Private Sub SetInitialRow()
            Dim dt As DataTable = New DataTable()
            Dim dr As DataRow = Nothing
            dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
            dt.Columns.Add(New DataColumn("Column1", GetType(String)))
            dt.Columns.Add(New DataColumn("Column2", GetType(String)))
            dt.Columns.Add(New DataColumn("Column3", GetType(String)))
            dr = dt.NewRow()
            dr("RowNumber") = 1
            dr("Column1") = String.Empty
            dr("Column2") = String.Empty
            dr("Column3") = String.Empty
            dt.Rows.Add(dr)
            ViewState("CurrentTable") = dt
            Gridview1.DataSource = dt
            Gridview1.DataBind()
        End Sub
    
        Private Sub AddNewRowToGrid()
            Dim rowIndex As Integer = 0
    
            If ViewState("CurrentTable") IsNot Nothing Then
                Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
                Dim drCurrentRow As DataRow = Nothing
    
                If dtCurrentTable.Rows.Count > 0 Then
    
                    For i As Integer = 1 To dtCurrentTable.Rows.Count
                        Dim box1 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
                        Dim box2 As DropDownList = CType(Gridview1.Rows(rowIndex).Cells(2).FindControl("DropDownList1"), DropDownList)
                        Dim box3 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)
                        drCurrentRow = dtCurrentTable.NewRow()
                        drCurrentRow("RowNumber") = i + 1
                        dtCurrentTable.Rows(i - 1)("Column1") = box1.Text
                        dtCurrentTable.Rows(i - 1)("Column2") = box2.Text
                        dtCurrentTable.Rows(i - 1)("Column3") = box3.Text
                        rowIndex += 1
                    Next
    
                    dtCurrentTable.Rows.Add(drCurrentRow)
                    ViewState("CurrentTable") = dtCurrentTable
                    Gridview1.DataSource = dtCurrentTable
                    Gridview1.DataBind()
                End If
            Else
                Response.Write("ViewState is null")
            End If
    
            ' SetPreviousData()
        End Sub
    
        Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs)
            
    
            AddNewRowToGrid()
    
    
        End Sub



    Sno Name Activity Count
    1 Mohammd acccc 5

    i have table called   xx    

    select count(*) from xx

    i want when i press add compare   count of xx table  with count       if  count in gridview  grater than count in xx table   error appears and not allow to add new row    

    if  count in in gridview  less than or equal than count of xx table    allow to add new row in gridview

    and also if  i want add 3rd  row    i want to make sum of count in row1 and 2  and compare them with xx table   if  grater    error appears    if less or equal can add

    Hope you understand  my point

    </div>

    Monday, March 15, 2021 8:46 PM

All replies

  • User1535942433 posted

    Hi thepast,

    According to your description,I don't understand your requirement clearly.In your codes,there are three four columns: RowNumber, HeadText1-3.

    I have some doubts:

    1.What is table xx?Is it a sql table in the database?

    2.What is count in gridview? Is the HeadText3 same with "Count" in your sample? Is the count same with TextBox3's value 5?

    Please tell more details.

    Best regards,

    Yijing Sun

    Tuesday, March 16, 2021 7:39 AM