簡體   English   中英

如何讀取 asp.net 中的 gridview 列和行數據?

[英]How to read gridview column and row wise data in asp.net?

我想讀取 c# 中的 gridview 列數據。

我使用 C# 作為后端和 ASP.Net 作為后端。

前端:asp網格視圖

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" onrowcommand="GridView2_RowCommand" DataKeyNames="ID" >
            <Columns>
               
                <asp:BoundField DataField="NAME" HeaderText="EVENT NAME" />
                <asp:BoundField DataField="TYPE" HeaderText="TYPE OF EVENT" />
                <asp:BoundField DataField="desc" HeaderText="DESCRIPTI0ON OF EVENT" />
                 <asp:TemplateField HeaderText="Poster">
                      <EditItemTemplate>
                          <asp:FileUpload ID="FileUpload" runat="server" />
                      </EditItemTemplate>
                      <ItemTemplate>
                          <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("poster") %>' Height="100px" Width="150px" />
                      </ItemTemplate>
                </asp:TemplateField>
                **<asp:TemplateField HeaderText="Team Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox" runat="server" ></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox" runat="server" ></asp:TextBox>
                    </ItemTemplate>**
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:ButtonField ButtonType="Button" Text="participate" CommandName="participate" HeaderText="participation" />
                </Columns>
        </asp:GridView>

c# 保護無效 GridView2_RowCommand

 protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["Event_ManagementConnectionString"].ConnectionString);
                SqlCommand cmd;
                int rowindex = Convert.ToInt32(e.CommandArgument);
                GridViewRow SelectedRow = GridView2.Rows[rowindex];
                string id = Convert.ToString(GridView2.DataKeys[rowindex].Values[0]);
                string team_name = SelectedRow.Cells[4].Text;
                //= Convert.ToString(GridView2.Rows[rowindex].Values[4]);
                Response.Write(team_name+"what the hell it is"+ SelectedRow.Cells[0].Text+SelectedRow.Cells[1].Text+ SelectedRow.Cells[2].Text+ SelectedRow.Cells[3].**Text+ SelectedRow.Cells[4].Text**);
                if (e.CommandName == "participate")
                {
                    if (team_name == "null")
                    {
                        Label3.Text = "Team name cant be blank";
                        Label3.Visible = true;
                    }
                }
            }
       }

如何從 gridview 中獲取團隊名稱? 我無法從第 4 列獲取數據:SelectedRow.Cells[4].Text;

團隊名稱保存在第 4 列的文本框控件中。因此,select 團隊文本控件,您應該將團隊名稱作為文本框的文本屬性。 像這樣更改代碼:

int rowindex = Convert.ToInt32(e.CommandArgument);
GridViewRow SelectedRow = GridView2.Rows[rowindex];
string id = Convert.ToString(GridView2.DataKeys[rowindex].Values[0]);

TextBox txtteam= (TextBox)SelectedRow.Cells[4].FindControl("TextBox");
string team_name = txtteam.Text;

//= Convert.ToString(GridView2.Rows[rowindex].Values[4]);
Response.Write(team_name + "what the hell it is" + SelectedRow.Cells[0].Text + SelectedRow.Cells[1].Text + SelectedRow.Cells[2].Text + SelectedRow.Cells[3].Text + txtteam.Text);
if (e.CommandName == "participate")
 {
    if (team_name == "null")
        {
            Label3.Text = "Team name cant be blank";
            Label3.Visible = true;
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM