问题描述
在gridview中动态绑定id。
代码如下
< asp:GridView ID =GridView1runat =serverAllowPaging =TrueAutoGenerateColumns =FalseCellPadding =10
CellSpacing =0OnRowDataBound =GridView1_RowDataBound
/>
< columns>
< asp:BoundField DataField =DateHeaderText =Date/>
< asp:BoundField DataField =LocationHeaderText =Location/>
< asp:BoundField DataField =TimeInHeaderText =TimeIn/>
< asp:BoundField DataField =TimeOutHeaderText =TimeOut/>
< asp:BoundField DataField =WorkingHoursHeaderText =WorkingHours/ >
< asp:TemplateField HeaderText =ViewAttendance>
< itemtemplate>
< asp:LinkButton ID =lnkdeletehref =#myModaldata-toggle =modalrunat =server>点击这里
< asp:TemplateField HeaderText =Id>
< itemtemplate>
<%# Container.DataItemIndex + 1%>
gridview行数据绑定代码如下
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem,Date) ));
LinkButton lnkbtnresult =(LinkButton)e.Row.FindControl(lnkdelete);
BindGrid2();
}
if(e.Row.RowType == DataControlRowType.DataRow)
{
int id =转换。 ToInt32(e.Row.Cells [6] .Text);
if(id == 2)
{
BindGrid3();
gvview2.Visible = true;
}
}
日期位置Timein TimeOut Workinghours ViewAttendance Id
1/1/2018 Chennai 9.30 6.30 9 Clickhere 1
2 // 12018 Chennai 9.30 7.30 10 Clickh ere 2
3/1/2018 Chennai 9.30 5.30 8 Clickhere 3
4/1/2018 Chennai 9.30 8.00 10.30 Clickhere 4
5 / 1/2018 Chennai 9.30 7.00 9.30 Clickhere 5
当我运行上面的代码时显示错误如下
输入字符串格式不正确
以上错误如下所示:
int id = Convert.ToInt32(e.Row.Cells [6] .Text);
上面代码中的错误是什么。
我尝试过:
dynamically binding id in gridview.
code as follows
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="10"
CellSpacing="0" OnRowDataBound="GridView1_RowDataBound"
/>
<columns>
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="TimeIn" HeaderText="TimeIn" />
<asp:BoundField DataField="TimeOut" HeaderText="TimeOut" />
<asp:BoundField DataField="WorkingHours" HeaderText="WorkingHours" />
<asp:TemplateField HeaderText="ViewAttendance">
<itemtemplate>
<asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Click Here
<asp:TemplateField HeaderText="Id">
<itemtemplate>
<%# Container.DataItemIndex + 1 %>
gridview row data bound code as follows
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Date"));
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
BindGrid2();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(e.Row.Cells[6].Text);
if (id == 2)
{
BindGrid3();
gvview2.Visible = true;
}
}
Date Location Timein TimeOut Workinghours ViewAttendance Id
1/1/2018 Chennai 9.30 6.30 9 Clickhere 1
2//12018 Chennai 9.30 7.30 10 Clickhere 2
3/1/2018 Chennai 9.30 5.30 8 Clickhere 3
4/1/2018 Chennai 9.30 8.00 10.30 Clickhere 4
5/1/2018 Chennai 9.30 7.00 9.30 Clickhere 5
When i run the above code shows error as follows
The input string was not in correct format
The above error shows in below line as follows
int id = Convert.ToInt32(e.Row.Cells[6].Text);
what is the mistake in my above code.
What I have tried:
dynamically binding id in gridview.
code as follows
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="10"
CellSpacing="0" OnRowDataBound="GridView1_RowDataBound"
/>
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="TimeIn" HeaderText="TimeIn" />
<asp:BoundField DataField="TimeOut" HeaderText="TimeOut" />
<asp:BoundField DataField="WorkingHours" HeaderText="WorkingHours" />
<asp:TemplateField HeaderText="ViewAttendance">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Click Here</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
gridview row data bound code as follows
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Date"));
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
BindGrid2();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(e.Row.Cells[6].Text);
if (id == 2)
{
BindGrid3();
gvview2.Visible = true;
}
}
Date Location Timein TimeOut Workinghours ViewAttendance Id
1/1/2018 Chennai 9.30 6.30 9 Clickhere 1
2//12018 Chennai 9.30 7.30 10 Clickhere 2
3/1/2018 Chennai 9.30 5.30 8 Clickhere 3
4/1/2018 Chennai 9.30 8.00 10.30 Clickhere 4
5/1/2018 Chennai 9.30 7.00 9.30 Clickhere 5
When i run the above code shows error as follows
The input string was not in correct format
The above error shows in below line as follows
int id = Convert.ToInt32(e.Row.Cells[6].Text);
what is the mistake in my above code.
推荐答案
int id = e.Row.RowIndex;
长答案是具有ID的单元格是模板字段,模板字段包含控件,而不是纯文本。如果你看一下定义;
The long answer is that the cell with the ID is a template field and template fields contain controls, not plain text. If you look at the definition;
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
你可以看到你实际上并没有添加任何控件,无论是asp。 net正在做一些工作并为你添加控件,它将你的ItemTemplate中的所有内容转换为asp:DataBoundLiteralControl。所以如果你使用调试器并查看
e.Row.Cells [6] .Controls
你会看到那里有一个控件,一个数据绑定文字,你可以用它来获得这个
You can see that you're not actually adding any controls, however asp.net is doing some work and adding the control for you, it turns everything in your ItemTemplate into an asp:DataBoundLiteralControl. So if you use the debugger and look at
e.Row.Cells[6].Controls
you'll see there is one control in there, a databound literal, which you can get using this
DataBoundLiteralControl l = (DataBoundLiteralControl) e.Row.Cells[6].Controls[0];
如果你看看l.Text,你现在可以看到单元格中显示的文本。但是你仍然无法将其转换为int,因为它在模板中包含* everything *,包括空格,换行符等。由于html忽略空格和行返回,因此在页面上看不到这些*,但Convert.ToInt32不会t。
你必须记住gridview是一种显示机制,你永远不应该读取单元格的内容来进行处理,因为数据总是以另一种方式可用,在您的特定情况下,数据在e.Row.RowIndex中已经作为int提供,因此您应该使用它而不是解析单元格文本。
if you look at "l.Text" you can now see the text that appears in the cell. However you still can't convert that to int because it contains *everything* in your template including white space, line returns etc. Those aren't seen on the page as html ignores white space and line returns, but Convert.ToInt32 doesn't.
You have to remember that the gridview is a display mechanism, you should never read the contents of the cells to do processing as the data is always available another way, in your specific case the data is available as an int already in e.Row.RowIndex so that is what you should use rather than parsing the cell text.
这篇关于输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!