0

我整天都在困扰我的 Gridview 完全有问题。我不经常使用 Gridviews,所以其中一些让我感到困惑,谷歌并没有像我希望的那样提供太多帮助。

我正在尝试将通过 LINQ 创建的对象绑定到 gridview,然后进行一些更新。

XAML:

<asp:GridView runat="server" ID="GV1"
        onrowediting="gridViewUsers_RowEditing"
        onrowcancelingedit="gridViewUsers_RowCancelingEdit"
        onrowdeleting="gridViewUsers_RowDeleting"
        onrowupdating="gridViewUsers_RowUpdating"
        >
    <Columns>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

代码:

private void BindGrid()
{
    List<Room> stuff = new List<Room>();
    Hotel = dc1.Rooms.ToList();

    GV1.DataSource = Hotel;
    GV1.DataBind();
}

protected void gridViewUsers_RowEditing(object sender, GridViewEditEventArgs e)
{
    GV1.EditIndex = e.NewEditIndex;
    BindGrid();
}

我只想从选定的行中获取 Room 对象,然后通过 LINQ 将其更新回数据库。但我似乎无法接触到那个物体。我的下一个尝试是从行中获取每个条目。所以我找到了类似下面示例的代码。但一切都是空的。

    protected void gridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string tenant = ((TextBox)(GV1.Rows[e.RowIndex].FindControl("Tenant"))).Text;
        string age = ((TextBox)(GV1.Rows[e.RowIndex].FindControl("Age"))).Text;
        string roomno = ((TextBox)(GV1.Rows[e.RowIndex].FindControl("Room_No"))).Text;            
        BindGrid();
    }
4

2 回答 2

0

试试这个代码来访问你的textbox值。

 protected void gridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         GridViewRow row = gridViewUsers.Rows[e.RowIndex];
        Textbox txtbxtenant = (TextBox)row.FindControl("Tenant");
         string tenanat = Convert.toString(tentant.text);
        Textbox txtbxAge =(Textbox)row.FindControl("Age");                   
        string age = Convert.toString(Age.text);
       // string roomno = ((TextBox)  
       //(GV1.Rows[e.RowIndex].FindControl("Room_No"))).Text;            
       // BindGrid();

    }
于 2013-11-15T10:57:01.727 回答
0

尝试设置 AutoGenerateColumns = false 并手动提供列。就像是:

<asp:GridView runat="server" ID="GV1"
    onrowediting="gridViewUsers_RowEditing"
    onrowcancelingedit="gridViewUsers_RowCancelingEdit"
    onrowdeleting="gridViewUsers_RowDeleting"
    onrowupdating="gridViewUsers_RowUpdating"
    AutoGenerateColumns = "false"
    >
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField HeaderText="Tenant" DataField="Tenant" />
        <asp:BoundField HeaderText="Age" DataField="Age" />
        <asp:BoundField HeaderText="Room_No" DataField="Room_No" />
    </Columns>
</asp:GridView>

编辑:请将此代码添加到您的解决方案中

public static int GetCellIndexByFieldHandle(this GridView grid, string fieldHandle)
{
    int iCellIndex = -1;

    for (int iColIndex = 0; iColIndex < grid.Columns.Count; iColIndex++)
    {
        if (grid.Columns[iColIndex] is DataControlField)
        {
            DataControlField col = (DataControlField)grid.Columns[iColIndex];
            if ((col is BoundField && string.Compare(((BoundField)col).DataField, fieldHandle, true) == 0)
                || string.Compare(col.SortExpression, fieldHandle, true) == 0
                || col.HeaderText.Contains(fieldHandle))
            {
                iCellIndex = iColIndex;
                break;
            }
        }
    }
    return iCellIndex;
}

/// <summary>
/// Gets the ordinal index of a TableCell in a rendered GridViewRow, using a text fieldHandle (e.g. the corresponding column's DataFieldName/SortExpression/HeaderText)
/// </summary>
public static int GetCellIndexByFieldHandle(this GridViewRow row, string fieldHandle)
{
    return GetCellIndexByFieldHandle((GridView)row.Parent.Parent, fieldHandle);
}

并尝试

string tenant = ((TextBox)GV1.Rows[e.RowIndex].Cells[GV1.Rows[e.RowIndex].GetCellIndexByFieldHandle("Tenant")].Controls[0]).Text;
于 2013-11-15T07:37:05.857 回答