Click here to Skip to main content
12,404,895 members (70,985 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
<tr>
       <td align="left" class="label" style="height: 23px" valign="top">
           <asp:Label ID="lbl_Scale_pointers" runat="server" Text="Scale pointers"></asp:Label></td>
       <td align="left" class="label" style="width: 1px; height: 23px;" valign="top">
           :</td>
       <td align="left" class="control" style="height: 23px">
           <%--<cc1:DropDownListControl ID="ddlst_Scale_pointers" runat="server">
           </cc1:DropDownListControl>--%>

Protected Sub ddlst_Scale_pointers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlst_Scale_pointers.SelectedIndexChanged
       BindGridview(ddlst_Scale_pointers.SelectedValue)
   End Sub
Sub BindGridview(ByVal rownum As Integer)
For i As Integer = 0 To rownum - 1   
 
    Dim lblTest As Label = CType(grdvw1.FindControl("lblPointerNo"), Label)
    lblTest.Text = i + 1 
    
Next
 
grdvw1.DataBind()
End sub
on selection of dropdownlist value i need number of rows for grid

I want to change the value of label dynamically that is if dropdwnlist has value 3 then the lblpionter no should display
1
2
3 in 3 diff rows so do any1 has the ans for this
Posted 11-Feb-13 23:57pm
Updated 12-Feb-13 6:52am
v6
Comments
PrachiMhatre 12-Feb-13 7:27am
   
Please clarify the requirement..
ryanb31 12-Feb-13 11:04am
   
Where are you stuck?
Karthik Harve 12-Feb-13 12:50pm
   
[Edit] pre tags added

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

change the gridview source as below.
<columns>
    <asp:templatefield headertext="Pointer No" xmlns:asp="#unknown">
    <itemtemplate>
    <asp:label text='<%#Eval("PointerNo")%>' id="lblPointerNo" runat="server">
    </asp:label>
    </itemtemplate>
    </asp:templatefield>
</columns>
bind the gidview as below.
Sub BindGridview(ByVal rownum As Integer)
Dim dt as New DataTable
dt.Columns.Add("PointerNo", Type.GetType(string))
dt.AcceptChanges()
 
For i As Integer = 0 To rownum - 1   
 dt.Rows.Add((i + 1).ToString())
 dt.AcceptChanges()
Next
 
grdvw1.DataSource = dt
grdvw1.DataBind()
End sub

option Two

keep the grid view source unchanged. add the below code in RowDataBoundEvent.

Dim lbl as Label = CType(e.Row.FindControl("lblPointerNo"), Label)
If lbl IsNot  Nothing THEN
    IF e.Row.RowIndex < Convert.ToInt32(ddlst_Scale_pointers.SelectedValue) THEN
        lbl.Text = (e.Row.RowIndex + 1).ToString();
    End If
End If

hope it helps
  Permalink  
v4
Comments
Member 9410081 13-Feb-13 1:28am
   
I am not able to get the values in label
Karthik Harve 13-Feb-13 1:31am
   
did you added the Eval function in gridview source ?
Member 9410081 13-Feb-13 1:40am
   
NO
Member 9410081 13-Feb-13 1:41am
   
ya i have added
Karthik Harve 13-Feb-13 3:31am
   
what is the error message ?
Member 9410081 13-Feb-13 3:59am
   
use new keyword
Member 9410081 13-Feb-13 4:01am
   
the control is gettin added as last colmn and i want it as first 1
Karthik Harve 13-Feb-13 4:20am
   
check with option two in updated answer.
Member 9410081 13-Feb-13 4:52am
   
it giving error <> sign
Karthik Harve 13-Feb-13 4:56am
   
ya. its my mistake. check updated one.
Member 9410081 13-Feb-13 5:08am
   
Object reference not set to an instance of an object.
Karthik Harve 13-Feb-13 5:13am
   
check this condition before that code.

If e.Row.RowType = DataControlRowType.DataRow Then
End If

so, that should be as below.

If e.Row.RowType = DataControlRowType.DataRow Then
Dim lbl as Label = CType(e.Row.FindControl("lblPointerNo"), Label)
If lbl IsNot Nothing THEN
IF e.Row.RowIndex < Convert.ToInt32(ddlst_Scale_pointers.SelectedValue) THEN
lbl.Text = (e.Row.RowIndex + 1).ToString();
End If
End If
End If
Karthik Harve 13-Feb-13 5:34am
   
If go with option two, no need to use that new column "PointerNo" and page source also you can keep unchanged. only RowDataBoundEvent will have that code to change the label text based on the row index.
Karthik Harve 13-Feb-13 6:27am
   
In that function, create one datatable with row count equal to dropdownlist selected value. and bind gridview with that datatable.
Karthik Harve 13-Feb-13 7:19am
   
bindgridview
Karthik Harve 13-Feb-13 12:54pm
   
did you bind the gridview with that datatable ? still not working ?
Member 9410081 13-Feb-13 23:42pm
   
what about the eval function?
Karthik Harve 14-Feb-13 0:07am
   
remove Eval function.
Member 9410081 13-Feb-13 23:54pm
   
What chages shuld i do over here
Sub BindGridview(ByVal rownum As Integer)
Dim dt as New DataTable
dt.Columns.Add("PointerNo", Type.GetType(string))
dt.AcceptChanges()

For i As Integer = 0 To rownum - 1
dt.Rows.Add((i + 1).ToString())
dt.AcceptChanges()
Next

grdvw1.DataSource = dt
grdvw1.DataBind()
End sub
Karthik Harve 14-Feb-13 0:14am
   
ya, its fine.
Karthik Harve 14-Feb-13 2:06am
   
The datatable does not add new column to gridview. it will just create the rows for the gridview.
Member 9410081 14-Feb-13 3:53am
   
ok

But the below code creates new column

dt.Columns.Add("PointerNo", Type.GetType(string))
dt.AcceptChanges()

For i As Integer = 0 To rownum - 1
dt.Rows.Add((i + 1).ToString())
dt.AcceptChanges()
Next
Karthik Harve 14-Feb-13 4:11am
   
Its just a dummy column, to know the number of rows for the gridview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100