Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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>--%>
           <asp:DropDownList ID="ddlst_Scale_pointers" runat="server" AutoPostBack="True" Width="80px">
           <asp:ListItem>1</asp:ListItem>
           <asp:ListItem>2</asp:ListItem>
           <asp:ListItem>3</asp:ListItem>
           <asp:ListItem>4</asp:ListItem>
           <asp:ListItem>5</asp:ListItem>
           <asp:ListItem>6</asp:ListItem>
           <asp:ListItem>7</asp:ListItem>
 
           </asp:DropDownList>
       </td>
   </tr>
   <tr>
   <td align ="center" class="label" style="height: 23px" valign="top">
   <asp:Label ID="lblcolor" runat="server" Text="Please pick the color for Pointer"></asp:Label>
   </td>
   </tr>
 
   <tr>
  <%--<td align="center" class="label" style="height: 23px" valign="top">
         <asp:Label Visible="false" ID="Label1" runat="server" Text=""></asp:Label></td>
 
       <td align="center"  class="label" style="width: 1px; height: 23px;" valign="top">
           </td>--%>
  <td align="center" colspan="3"  class="control" style="height: 23px" valign="top"  >
 
  <asp:GridView  ID="grdvw1" EnableViewState="true" OnRowDataBound="grdvw1_RowDataBound" runat="server" Width="300px" CellSpacing="3" CellPadding ="3">
 

   <Columns>
           <asp:TemplateField HeaderText="Pointer No">
           <ItemTemplate>
           <asp:Label Text="" ID="lblPointerNo" runat="server" >
           </asp:Label>
           </ItemTemplate>
           </asp:TemplateField>
   </Columns>
 
   <Columns>
           <asp:TemplateField HeaderText="Pointer Color">
           <ItemTemplate>
           <asp:TextBox ID="txtcolor" runat="server" class="color" MaxLength="19" Width="200px"></asp:TextBox>
           </ItemTemplate>
           </asp:TemplateField>
   </Columns>
 
    </asp:GridView>
 
   </td>
 
   </tr>
 
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 12-Feb-13 0:57am
Edited 12-Feb-13 7:52am
v6
Comments
PrachiMhatre at 12-Feb-13 7:27am
   
Please clarify the requirement..
ryanb31 at 12-Feb-13 11:04am
   
Where are you stuck?
Karthik Harve at 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 at 13-Feb-13 1:28am
   
I am not able to get the values in label
Karthik Harve at 13-Feb-13 1:31am
   
did you added the Eval function in gridview source ?
Member 9410081 at 13-Feb-13 1:40am
   
NO
Member 9410081 at 13-Feb-13 1:41am
   
ya i have added
Karthik Harve at 13-Feb-13 3:31am
   
what is the error message ?
Member 9410081 at 13-Feb-13 3:59am
   
use new keyword
Member 9410081 at 13-Feb-13 4:01am
   
the control is gettin added as last colmn and i want it as first 1
Karthik Harve at 13-Feb-13 4:20am
   
check with option two in updated answer.
Member 9410081 at 13-Feb-13 4:52am
   
it giving error <> sign
Karthik Harve at 13-Feb-13 4:56am
   
ya. its my mistake. check updated one.
Member 9410081 at 13-Feb-13 5:08am
   
Object reference not set to an instance of an object.
Karthik Harve at 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 at 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 at 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 at 13-Feb-13 7:19am
   
bindgridview
Karthik Harve at 13-Feb-13 12:54pm
   
did you bind the gridview with that datatable ? still not working ?
Member 9410081 at 13-Feb-13 23:42pm
   
what about the eval function?
Karthik Harve at 14-Feb-13 0:07am
   
remove Eval function.
Member 9410081 at 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 at 14-Feb-13 0:14am
   
ya, its fine.
Karthik Harve at 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 at 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 at 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
0 OriginalGriff 7,215
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
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