C# 如何从 GridView 的模板字段中获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13795468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:48:51  来源:igfitidea点击:

How to get values from template fields in GridView?

c#asp.netexcelgridviewtemplatefield

提问by Sandeep

This is my markup of GridView.

这是我对 GridView 的标记。

<Columns>
    <asp:TemplateField HeaderText="Customer Name">
        <ItemTemplate>
            <asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Customer.Name")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="PickUpPoint">
        <ItemTemplate>
            <asp:Label ID="lblPickUpPoint" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Pickuppoint")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

I have a button which stores the values in the worksheet cells of excel object.

我有一个按钮,用于将值存储在 excel 对象的工作表单元格中。

for (int i = 0; i < GridView2.Rows.Count; i++)
{
    for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++)
    {
        xlWorkSheet.Cells[i + 1, j + 1] = GridView2.Rows[i].Cells[j].Text;
   }
}

How do I get the values of GridView and store it in a worksheet, as the GridView2.Rows[i].Cells[j].Text returns empty string.

我如何获取 GridView 的值并将其存储在工作表中,因为GridView2.Rows[i].Cells[j].Text 返回空字符串。

采纳答案by Cdeez

Your are missing a type cast. Do it like this-

您缺少类型转换。像这样做-

Label name = (Label)GridView2.Rows[i].Cells[j].FindControl("lblname");
xlWorkSheet.Cells[i + 1, j + 1] = name.Text;

Update-If you can name your labels as Label0 and Label1, then in the second for loop-

更新-如果您可以将标签命名为 Label0 和 Label1,则在第二个 for 循环中-

for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++)
  {
     Label xyz = (Label)GridView2.Rows[i].Cells[j].FindControl("Label"+j);
     xlWorkSheet.Cells[i + 1, j + 1] = xyz.Text;
  }

For Header text- string hText = GridView2.HeaderRow.Cells[your column number].Text;

对于标题文本- string hText = GridView2.HeaderRow.Cells[your column number].Text;

回答by sajanyamaha

For retrieving values do this:

要检索值,请执行以下操作:

for (int i = 0; i < GridView2.Rows.Count; i++)
{
  //extract the TextBox values
  Label lblname= (Label)GridView2.Rows[i].Cells[0].FindControl("lblname");
  Label lblPickUpPoint= (Label)GridView2.Rows[i].Cells[0].FindControl("lblPickUpPoint");
  //Do your excel binding here
}

回答by Clarice Bouwer

{cell}.Textwill only work if there isn't a control within the TemplateField. You have added a label to your template which is why you first need to find the control, cast your object to the control and access the control's properties as needed.

{cell}.Text仅当TemplateField. 您已向模板添加了一个标签,这就是为什么您首先需要找到控件、将对象转换为控件并根据需要访问控件的属性。

If you want a more generic approach you could always do the following (remove the label control and simply add the evaluated field):

如果您想要更通用的方法,您可以始终执行以下操作(删除标签控件并简单地添加评估字段):

<Columns>
    <asp:TemplateField HeaderText="Customer Name">
        <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "Customer.Name")%>
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="PickUpPoint">
         <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "Pickuppoint")%>
         </ItemTemplate>
     </asp:TemplateField>
</Columns>

When you use the code you initially used, the {cell}.Textshould no longer return empty.

当您使用最初使用的代码时,{cell}.Text不应再返回空。

回答by Abdul Khaliq

 protected void Button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        for (i = 0; i <= GvSchedule.Rows.Count - 1; i++)
        {
            if (((CheckBox)GvSchedule.Rows[i].FindControl("ChkIsService")).Checked)
            {
                string catName = ((Label)GvSchedule.Rows[i].FindControl("lblCatName")).Text;
                var subCatName = ((Label)GvSchedule.Rows[i].FindControl("lblSubCatName")).Text;
            }
        }
     }

回答by Nitzan Malichi

Try using this code, I had a similar problem. I think this code is more dynamic and you do not have to find the name of the label every time. But to keep the correspondence between the controls And the index Controls [ ]:

尝试使用此代码,我遇到了类似的问题。我认为这段代码更具动态性,您不必每次都找到标签的名称。但是要保持控件和索引控件[]之间的对应关系:

for (int row = 1; row <= totalRows; row++)
{
    for (int col = 0; col < totalCols; col++)
    {
        if (GridView1.Columns[col].Visible)
        {
            if (String.IsNullOrEmpty(GridView1.Rows[row - 1].Cells[col].Text))
            {
                if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("Label"))
                {
                    Label LB = (Label)GridView1.Rows[row - 1].Cells[col].Controls[1];
                    workSheet.Cells[row + 1, col + 1].Value = LB.Text;
                }
                else if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("LinkButton"))
                {
                    LinkButton LB = (LinkButton)GridView1.Rows[row - 1].Cells[col].Controls[1];
                    workSheet.Cells[row + 1, col + 1].Value = LB.Text;
                }
            }
            else
            {
                workSheet.Cells[row + 1, col + 1].Value = GridView1.Rows[row - 1].Cells[col].Text;
            }