本文介绍了如何将行添加到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要添加编号.这样将行的数量添加到gridview,但是iam面临添加第二行的问题.
I want add the no. of rows to gridview like this way but iam facing the problem adding second row.
DataTable dt = new DataTable();
try
{
SqlCommand comm = new SqlCommand();
SqlConnection conn = new SqlConnection();
SqlDataReader dr;
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("") + @"\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
conn.Open();
comm.Connection = conn;
comm.CommandText = "select name,salary from tulasi ";
SqlDataAdapter da = new SqlDataAdapter("select name,salary from tulasi ", conn);
da.Fill(dt);
comm.ExecuteNonQuery();
dr = comm.ExecuteReader();
// }
// DataTable GetData()
//{
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("sal", typeof(int)));
//dt.Columns.Add(new DataColumn("Edit", typeof(HyperLink)));
dt.Columns.Add(new DataColumn("Delete", typeof(string)));
if (dr.Read())
{
DataRow dr1 = dt.NewRow();
dr1["Name"] = dr.GetString(0);
Label1.Text = dr.GetString(0);
Label2.Text = dr.GetInt32(1).ToString();
dr1["sal"] = dr.GetInt32(1);
//dr1["Edit"] = ResolveUrl("~/App_Data/Autumn.jpg");
dr1["Delete"] = ResolveUrl("Default2.aspx?sal=" + Label2.Text + "");
dt.Rows.Add(dr1);
推荐答案
GridView g = new GridView();
Page.Controls.Add(g);
g.DataSource = dt;
<asp:UpdatePanel runat="server" ID="UPAssets">
<ContentTemplate>
<asp:DataList ID="dlstCreateAssets" runat="server" Width="100%" OnItemDataBound="dlstCreateAssets_ItemDataBound">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center">
<div id="content_div">
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<tr>
<td align="left" style="padding-left: 30px;" id="id_td_pageHeading">
<asp:Label ID="lblName" runat="server" Text="Create/Modify Asset" Font-Bold="True"
Font-Size="Medium" CssClass="cslTitle"></asp:Label>
</td>
<td align="right">
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
(*) Fields Mandatory
</td>
<td align="left">
<asp:Label ID="lblMsg" ForeColor="Red" runat="server" Text="" Visible="false"></asp:Label>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
Item Id
</td>
<td align="left">
<asp:TextBox ID="txtItemId" runat="server" Width="170px" Text='<%#Eval("ItemId")%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtItemId"
ErrorMessage="Enter ItemId" ValidationGroup="usctrl1btn2">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ValidationGroup="usctrl1btn2" ID="RegularExpressionValidator4"
runat="server" ErrorMessage="Enter only numeric value in Itemid." ControlToValidate="txtItemId"
SetFocusOnError="True" ValidationExpression="[0-9]*">*</asp:RegularExpressionValidator>
<asp:Button ID="btnCheckAvailability" runat="server" CssClass="asset-viewer-btn"
OnClick="btnCheckAvailability_Click" Text="Check Availability" />
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
Type*
</td>
<td align="left">
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="AssetTypeName"
DataValueField="AssetTypeId" Width="170px">
</asp:DropDownList>
<asp:HiddenField runat="server" ID="Drop1" Value='<%#Eval("AssetType")%>' />
<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator2"
runat="server" ControlToValidate="DropDownList1" ErrorMessage="Select Type" InitialValue="-1">*</asp:RequiredFieldValidator>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
SubType*
</td>
<td align="left">
<asp:DropDownList ID="DropDownList2" runat="server" DataTextField="AssetSubTypeName"
DataValueField="AssetSubTypeId" Width="170px">
</asp:DropDownList>
<asp:HiddenField runat="server" ID="Drop2" Value='<%#Eval("AssetSubType")%>' />
<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator3"
runat="server" ControlToValidate="DropDownList2" ErrorMessage="Select SubType"
InitialValue="-1">*</asp:RequiredFieldValidator>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
AssetName*
</td>
<td align="left">
<asp:TextBox ID="txtAssetName" runat="server" Width="170px" Text='<%#Eval("AssetName")%>'></asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Enter Asset Name" ControlToValidate="txtAssetName">*</asp:RequiredFieldValidator>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
Asset Data
</td>
<td align="left">
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("AssetData")%>' Height="114px"
TextMode="MultiLine" Width="249px"></asp:TextBox>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
</td>
<td align="left">
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
<asp:ValidationSummary ValidationGroup="usctrl1" ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False" />
</td>
<td align="left">
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" CssClass="asset-viewer-btn" />
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
<asp:ValidationSummary ID="ValidationSummary2" runat="server" ShowMessageBox="True"
ShowSummary="False" ValidationGroup="usctrl1btn2" />
</td>
<td align="left">
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
</td>
<td align="left">
<asp:Label ID="lblshow" ForeColor="Red" runat="server" Visible="False"></asp:Label>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
</td>
<td align="right">
</td>
<td align="left">
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
这篇关于如何将行添加到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!