【问题标题】:How to read gridview column and row wise data in asp.net?如何在 asp.net 中读取 gridview 列和行数据?
【发布时间】:2020-06-28 06:39:40
【问题描述】:

我想在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# protected void 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;

【问题讨论】:

    标签: c# asp.net gridview datagridview textbox


    【解决方案1】:

    团队名称保存在第 4 列的文本框控件中。因此,选择团队文本控件,您应该将团队名称作为文本框的文本属性。像这样更改代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多