[英]Question about Gridview and data in Asp.net with C#
我正在制作一個模塊來將數組保存在 SQL 數據庫中。 例如,我想保存(889,4,01/12/2021)
、 (889,4,02/12/2021)
和(889,4,03/12/2021)
。
我正在使用 gridview 獲得第一個值( 889
)。 然后我使用文本框獲取日期並運行查詢以按行返回日期並存儲在 gridview 中。
我正在嘗試使用GridView2.Rows[0].Cells[1].Text
選擇第二個 gridview 值( date
),但超出了有效值的范圍。
由於這是一個數組,我將所有 SQL 語句保存在一個文本框中,稍后我執行,這就是我的代碼
string[,] arreglo = new string[GridView1.Rows.Count, 7];
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkbox1 = (CheckBox)row.FindControl("chkActive");
if (chkbox1.Checked)
{
arreglo[row.RowIndex, 0] = GridView1.Rows[row.RowIndex].Cells[1].Text;
string[,] array = new string[GridView2.Rows.Count, 2];
foreach (GridViewRow col in GridView2.Rows)
array[col.RowIndex, 0] = GridView2.Rows[col.RowIndex].Cells[1].Text;
txtInsert.Text = txtInsert.Text + "insert into T_USUARIO_dETALLE(id_usuario,campana,fecha,fecha_carga,id_superv,estado_dotacion) values ('" + arreglo[row.RowIndex, 0].ToString() + "', '" + lblcampana.Text + "','"+ GridView2.Rows[0].Cells[1].Text + "','" + LBLSUPERV.Text + "','" + ddlEstado.SelectedValue + "')";
}
}
謝謝您的幫助!!!
好的,這是 db land 中最經典的設置之一!
我們有一些父記錄,對於每條記錄,我們需要顯示(和編輯)子行。
現在有更多的用戶界面選擇,然后是冰淇淋口味。
這表明我們需要“嵌套”主記錄(比如在網格中)以顯示子記錄(我們的第二個網格)。
好吧,事實證明網格真的不能很好地嵌套。
因此,對於父記錄,讓我們使用 ListView - 它們工作得更好。
好的,所以我們可以啟動向導 - 構建 LV,然后在這里 GO NUCLEAR 武器並刪除 + 炸毀所有模板。 當我們添加這個時,刪除額外的嵌套表。 我數了<2分鍾的時間。
所以,我們現在有了這個簡單的 LV。
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" >
<ItemTemplate>
<tr>
<td><asp:Button ID="cmdView" runat="server" Text="+" /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:Label ID="ProvinceLabel" runat="server" Text='<%# Eval("Province") %>' /></td>
<td><asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" class = "table table-hover" >
<tr runat="server" style="">
<th runat="server">View</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Province</th>
<th runat="server">Description</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
我們要填寫的代碼是這樣的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadMainGrid();
}
void LoadMainGrid()
{
string strSQL = "SELECT * FROM tblHotels ORDER BY HotelName";
ListView1.DataSource = MyRst(strSQL);
ListView1.DataBind();
}
我們現在有了這個:
好的,所以現在我們需要子網格。
然后我們將這個作為子網格移動到上面的 LV 中。
所以,現在我們有了這個標記:
<tr>
<td colspan="5">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover borderhide" style="display:none;width:97%;margin-left:2%" >
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="FirstName" runat="server" Text='<%# Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="LastName" runat="server" Text='<%# Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:TextBox ID="City" runat="server" Text='<%# Eval("City") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start date">
<ItemTemplate>
<asp:TextBox ID="dtStart" runat="server" Text='<%# Eval("dtStart", "{0:yyyy-MM-ddTHH:mm}") %>' TextMode="DateTimeLocal" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End date">
<ItemTemplate>
<asp:TextBox ID="dtEnd" runat="server" Text='<%# Eval("dtEnd","{0:yyyy-MM-ddTHH:mm}") %>' TextMode="DateTimeLocal" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</ItemTemplate>
因此,我們在 lv 列的標記之后立即進入上面。
現在,我們所要做的就是連接“+”按鈕來展開:
那是 LV 中的第一行按鈕
<td><asp:Button ID="cmdView" runat="server" Text="+" OnClick="cmdView_Click"/></td>
代碼是這樣的:
protected void cmdView_Click(object sender, EventArgs e)
{
Button cmd = (Button)sender;
ListViewDataItem gVR = (ListViewDataItem)cmd.NamingContainer;
GridView gChild = (GridView)gVR.FindControl("GridView2"); // pluck out the grid for this row
if (gChild.Style["display"] == "normal")
{
// if grid is already display, then hide it, and exit
gChild.Style["display"] = "none";
return;
}
gChild.Style["display"] = "normal";
int HotelPK = (int)ListView1.DataKeys[gVR.DataItemIndex]["ID"];
// only re-load if never loaded
if (gChild.Rows.Count == 0)
{
gChild.DataSource = MyRst("SELECT * from People where hotel_id = " + HotelPK);
gChild.DataBind();
}
}
所以,現在我們有了這個:
好的,所以現在我們要做的就是創建一個“保存”按鈕來保存我們對這個 GV 所做的任何更改。
這很容易,所以我們像這樣放入一個保存按鈕:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.