简体   繁体   English

关于 Gridview 和 Asp.net 和 C# 中的数据的问题

[英]Question about Gridview and data in Asp.net with C#

I'm making a module to save an array in an SQL database.我正在制作一个模块来将数组保存在 SQL 数据库中。 For example, I want to save (889,4,01/12/2021) , (889,4,02/12/2021) , and (889,4,03/12/2021) .例如,我想保存(889,4,01/12/2021)(889,4,02/12/2021)(889,4,03/12/2021)

I'm using a gridview where I obtain the first value ( 889 ).我正在使用 gridview 获得第一个值( 889 )。 Then I get the date with a textbox and I run a query to return the dates in rows and are stored in a gridview.然后我使用文本框获取日期并运行查询以按行返回日期并存储在 gridview 中。

I'm trying to choose the 2nd gridview value ( date ) with GridView2.Rows[0].Cells[1].Text , but is outside of the range of valid values.我正在尝试使用GridView2.Rows[0].Cells[1].Text选择第二个 gridview 值( date ),但超出了有效值的范围。

As this is an array, I save all the SQL sentences in a textbox an later I execute, so that is my code由于这是一个数组,我将所有 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 + "')";
    }
}

Thanks for the help!!!谢谢您的帮助!!!

Ok, so this is one of the most classic setups in db land!好的,这是 db land 中最经典的设置之一!

We have some parent records, and for each record, we need to display (and edit) child rows.我们有一些父记录,对于每条记录,我们需要显示(和编辑)子行。

Now there are more UI choices for this then flavors of ice-cream.现在有更多的用户界面选择,然后是冰淇淋口味。

And this suggests we need to "nest" the master records (say in a grid) to display of the child reocrds (our 2nd grid).这表明我们需要“嵌套”主记录(比如在网格中)以显示子记录(我们的第二个网格)。

Well, it turns out that grids REALLY don't nest well.好吧,事实证明网格真的不能很好地嵌套。

So, for parent record(s), lets use a ListView - they work much better.因此,对于父记录,让我们使用 ListView - 它们工作得更好。

Ok, so we can fire up the wizards - build the LV, and THEN GO NUCLEAR weapons here and delete + blow out ALL the templates.好的,所以我们可以启动向导 - 构建 LV,然后在这里 GO NUCLEAR 武器并删除 + 炸毁所有模板。 And while we add this, delete the extra nested table.当我们添加这个时,删除额外的嵌套表。 I count < 2 minutes of time.我数了<2分钟的时间。

So, we now have this simple LV.所以,我们现在有了这个简单的 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>

And our code to fill is this:我们要填写的代码是这样的:

   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();
    }

And we now have this:我们现在有了这个:

在此处输入图像描述

Ok, so now we need the child grid.好的,所以现在我们需要子网格。

Then we MOVE this to be child grid into the above LV.然后我们将这个作为子网格移动到上面的 LV 中。

So, now we have this markup:所以,现在我们有了这个标记:

<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>

So we dropped in above right after the markup for the lv columns.因此,我们在 lv 列的标记之后立即进入上面。

Now, all we have to do is wire up the the "+" button to expand:现在,我们所要做的就是连接“+”按钮来展开:

That was the first row button in the LV那是 LV 中的第一行按钮

 <td><asp:Button ID="cmdView" runat="server" Text="+"  OnClick="cmdView_Click"/></td>

And the code is this:代码是这样的:

    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();
        }
    }

So, now we have this:所以,现在我们有了这个:

在此处输入图像描述

Ok, so now all we have to do is create a "save" button that saves any changes we made to this GV.好的,所以现在我们要做的就是创建一个“保存”按钮来保存我们对这个 GV 所做的任何更改。

that is quite easy, and so we drop in a save button like this:这很容易,所以我们像这样放入一个保存按钮:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM