Hi ,
How to populate the data row by row in grid view.
For example:
I wirte this code in .aspx page
<asp:TemplateField HeaderText="COC Date of Calibration">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# COC_Date("strDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# COC_Date("strDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>The TemplateField will call the function from aspx.vb
Function COC_Date(ByVal strDate As String) As Date
For Each dr As DataRow In tempTbl.Rows ' I open very row of the data, get the data primary key then retrieved the strDate. Because the strDate is not in database
if .... the
strDate=....
else
strDate=.....
end if
' how can I populate the strDate in gridView row by row
' i think we need to do some coding at here, to populate it
end function
For example, in VB we can populate the data in Excel by this code:
objWB.Cells(a, 1) = strDate
So i need to do same thing to populate in grid view.
Any one have a suggestion????
looking for your expertise.
Thanks....
![]() |
0 |
![]() |
you can call a function
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text = <%# changevaluue(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "yourdatecolumn")%>'/>
</ItemTemplate>
</asp:TemplateField>
then in code behind
public string GetVisible(datetime dt)
{
check the conditions and return the value
}
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
Thanks for your reply.
But i cannot get you....
U mean i have to write Text = <%# changevaluue(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "yourdatecolumn")%>'/> instead of Text='<%# COC_Date("strDate") %>'
If I change the code as you suggest, then i cannot calll function COC_Date and cannot get strDate....
Did you have any suggest that i can continue my code to populate the strDate in Function COC_Date
Function COC_Date(ByVal strDate As String) As Date
For Each dr As DataRow In tempTbl.Rows ' I open very row of the data, get the data primary key then retrieved the strDate. Because the strDate is not in database
if .... the
strDate=....
else
strDate=.....
end if
' how can I populate the strDate in gridView row by row
' i think we need to do some coding at here, to populate it
end function
![]() |
0 |
![]() |
Can I write the code in this way.
Function COC_Date(ByVal strDate As String) As Date
For Each dr As DataRow In tempTbl.Rows ' I open very row of the data, get the data primary key then retrieved the strDate. Because the strDate is not in database
if .... the
strDate=....
else
strDate=.....
end if
Dim i as integer
GridView.Row(i).Cells(9).Text=strDate
end function
![]() |
0 |
![]() |
Hi gtcr ,
gtcr:
Text='<%# COC_Date("strDate") %>'The return value of COC_Date function will be set to Text property of TextBox2 in each line. But if you want to set value for each row of GridView , usually we do not put code here. I will put code in PreRender event handler of Gridview. Something like this:
protected void GridView1_PreRender(object sender, EventArgs e) { for (int i = 0; i < this.GridView1.Rows.Count; i++) { Label lbl = this.GridView1.Rows[i].FindControl("Label1") as Label; lbl.Text = "test"; } }<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="countryid" DataSourceID="SqlDataSource1" OnPreRender="GridView1_PreRender" Width="304px"> <Columns> <asp:BoundField DataField="countryid" HeaderText="countryid" ReadOnly="True" SortExpression="countryid" /> <asp:BoundField DataField="countryname" HeaderText="countryname" SortExpression="countryname" /> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
![]() |
0 |
![]() |