C# 如何在自动生成的GridView中获取标题名

C# 如何在自动生成的GridView中获取标题名,c#,asp.net,gridview,C#,Asp.net,Gridview,如何在自动生成的网格视图中获取标题名,在按钮中获取标题名。标题列不能始终相同 因此,根据列标题,我们必须进一步处理。请帮助查找列标题 我们正在通过下面的代码查找行索引 var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex; 像这样。那么,当在GridView中单击链接按钮时,如何查找GridView的列索引呢 " stringdt1=txtFromDate.Text; 字符串d1,d5=“”,d3=“”

如何在自动生成的
网格视图中获取标题名,在
按钮中获取标题名。标题列不能始终相同

因此,根据列标题,我们必须进一步处理。请帮助查找列标题

我们正在通过下面的代码查找行索引

var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
像这样。那么,当在
GridView
中单击链接按钮时,如何查找
GridView
的列索引呢

"

stringdt1=txtFromDate.Text;
字符串d1,d5=“”,d3=“”,d4=“”,日期,d6,服务日期;
如果(dt1!=“”)
{
d1=dt1;
如果(d1.包含(“.”)
{
字符串[]word=d1.Split('.');
d5=字[0];
d3=单词[1];
d4=单词[2];
}
else if(d1.包含(“-”)
{
字符串[]word=d1.Split('-');
d5=字[0];
d3=单词[1];
d4=单词[2];
}
else if(d1.包含(“/”)
{
string[]word=d1.Split('/');
d5=字[0];
d3=单词[1];
d4=单词[2];
}
日期=d4+“/”+d3+“/”+d5;
服务日期=d5+“-”+d3+“-”+d4;
}
其他的
{
日期=”;
服务日期=”;
}
字符串dt2=txtToDate.Text;
字符串t1,t5=“”,t3=“”,t4=“”,d2,t6,serv;
如果(dt1!=“”)
{
t1=dt2;
if(t1.包含(“.”)
{
字符串[]word=t1.Split('.');
t5=字[0];
t3=单词[1];
t4=单词[2];
}
else if(t1.包含(“-”))
{
string[]word=t1.Split('-');
t5=字[0];
t3=单词[1];
t4=单词[2];
}
else if(t1.包含(“/”)
{
string[]word=t1.Split('/');
t5=字[0];
t3=单词[1];
t4=单词[2];
}
d2=t4+“/”+t3+“/”+t5;
serv=t5+“-”+t3+“-”+t4;
//d5=d3+“/”+d6+“/”+d4;
}
其他的
{
d2=“”;
serv=“”;
}
dttest.Columns.Add(“机器名称日期”);
ArrayList数组_machine=新的ArrayList();
使用(con=newsqlconnection(con_str))
{
con.Open();
字符串qry;

qry=“从tb_reqmach中选择distinct mname,其中fromdate>=”“+date+””和todate您可以使用此代码段。它循环单击按钮所在行中的所有单元格,并尝试查找正确的列

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //get the control that fired the method
    Control control = e.CommandSource as Control;

    //get the row containing the control
    GridViewRow gvr = control.NamingContainer as GridViewRow;

    //get the row number
    int rowNumber = gvr.RowIndex;

    //declare the column variable
    int columnNumber = -1;

    //loop all the columns in the gridview
    for (int i = 0; i < GridView1.Columns.Count; i++)
    {
        //try to find the button that was clicked in each individual cell
        Button button = GridView1.Rows[rowNumber].Cells[i].FindControl(control.ID) as Button;

        //if the button is found set the column number
        if (button != null)
        {
            columnNumber = i;
        }
    }

    //get the column name
    Label1.Text = GridView1.HeaderRow.Cells[columnNumber].Text;
}
protectedvoid GridView1\u行命令(对象发送方,GridViewCommandEventArgs e)
{
//获取触发该方法的控件
控制=e.CommandSource作为控制;
//获取包含控件的行
GridViewRow gvr=control.NamingContainer作为GridViewRow;
//获取行号
int rowNumber=gvr.RowIndex;
//声明列变量
int columnNumber=-1;
//循环gridview中的所有列
对于(int i=0;i
GridView示例

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

您可以使用此代码段。它循环单击按钮所在行中的所有单元格,并尝试查找正确的列

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //get the control that fired the method
    Control control = e.CommandSource as Control;

    //get the row containing the control
    GridViewRow gvr = control.NamingContainer as GridViewRow;

    //get the row number
    int rowNumber = gvr.RowIndex;

    //declare the column variable
    int columnNumber = -1;

    //loop all the columns in the gridview
    for (int i = 0; i < GridView1.Columns.Count; i++)
    {
        //try to find the button that was clicked in each individual cell
        Button button = GridView1.Rows[rowNumber].Cells[i].FindControl(control.ID) as Button;

        //if the button is found set the column number
        if (button != null)
        {
            columnNumber = i;
        }
    }

    //get the column name
    Label1.Text = GridView1.HeaderRow.Cells[columnNumber].Text;
}
protectedvoid GridView1\u行命令(对象发送方,GridViewCommandEventArgs e)
{
//获取触发该方法的控件
控制=e.CommandSource作为控制;
//获取包含控件的行
GridViewRow gvr=control.NamingContainer作为GridViewRow;
//获取行号
int rowNumber=gvr.RowIndex;
//声明列变量
int columnNumber=-1;
//循环gridview中的所有列
对于(int i=0;i
GridView示例

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


对象引用未设置为对象的实例。LinkButton button=GridView4。行[rowNumber]。单元格[i]。FindControl(control.ID)作为LinkButton;In(control.ID)ID返回nullcontrol有值25请参见我的GridView示例对象引用未设置为对象的实例。LinkButton button=GridView4。行[rowNumber]。单元格[i]。FindControl(control.ID)作为LinkButton;In(control.ID)ID返回nullcontrol has value 25请参见我的GridView示例