Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编辑绑定到datagrid的数据?_C#_Asp.net_Datagrid - Fatal编程技术网

C# 如何编辑绑定到datagrid的数据?

C# 如何编辑绑定到datagrid的数据?,c#,asp.net,datagrid,C#,Asp.net,Datagrid,关于。我在datagrid中的列有Customer#、Description和Link 我已经设置了一个在rowDataBound上调用的函数,但是如何检索该行中的链接以便编辑它,然后将其重新绑定到datagrid protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e ) { if ( e.Row.RowIndex == 2 ) { } } 这是我的gridview代码

关于。我在datagrid中的列有Customer#、Description和Link

我已经设置了一个在rowDataBound上调用的函数,但是如何检索该行中的链接以便编辑它,然后将其重新绑定到datagrid

protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if ( e.Row.RowIndex == 2 )
    {

    }
}
这是我的gridview代码

<asp:GridView ID="grdLinks" runat="server" AutoGenerateColumns="False" DataSourceID="ldsCustomerLinks"
            OnRowDataBound="grdLinks_RowDataBound" EmptyDataText="No data was returned."
            DataKeyNames="ID" OnRowDeleted="grdLinks_RowDeleted" Width="80%" BackColor="White"
            HorizontalAlign="Center" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:HyperLinkField DataTextField="Link" HeaderText="Link" SortExpression="Link" DataNavigateUrlFields="Link" Target="_blank" />
            </Columns>
</asp:GridView>

<asp:LinqDataSource ID="ldsCustomerLinks" runat="server" ContextTypeName="ComplianceDataContext"
            TableName="CustomerLinks" EnableDelete="true">
</asp:LinqDataSource>

如果我理解正确,您需要获取名为Link的数据项的值。如果是这样的话,那么类似的方法应该会起作用:

编辑:我想你说的是,你想从数据库中获取值
链接
,对其进行操作,然后将
超链接的Url设置为新的、经过操作的值,如果是这样的话,它将如下所示:

ASPX页面(更新以反映发布的代码)


对于GridView中的每一行(包括页眉、页脚等),都会调用RowDataBound。因此,您应该从只检查包含数据的行开始

一旦你排成一行,有几种方法可以检查细胞。一种是只使用单元索引(这里是2)。在你的情况下,这似乎很简单,但如果你重新安排专栏的话,这将导致挫败感

以下是一个例子:


您可能还想考虑让gridview为您做这件事

您可以使用datanavigateurlformatstring属性插入querystring参数,如果您正试图这样做的话


是的,这正是我想要的。但是,修改后如何在datagrid中显示更新的链接?假设链接控件在网格中定义,则您希望执行DOK提到的FindControl,并使用您获取的值进行设置。FindControl被设置为等于超链接变量。如何将超链接变量转换为字符串,然后再转换回超链接?如果它清除了任何内容,我已经添加了gridview代码。hlParent继续返回null,尽管我将其更改为HyperLink Link=(HyperLink).e.Row.FindControl(“Link”),因为您试图覆盖数据绑定行为,所以您需要将模板字段添加到网格中。此外,hlParent为null,因为您需要控件上的
ID
为“hlParent”。我不太确定如何从FindControl()方法编辑它。要从一个超链接转到另一个字符串并返回到超链接,我该怎么做?在上面的代码段中,您有一个对超链接对象hlParent的引用。您现在可以编写诸如hlParent.NavigateURL=“someURL”之类的代码。在您的代码中,只需键入hlParent和一个点,leet ntellisense就会显示您的选项。我尝试了这个方法,但它给了我一个错误:“对象引用未设置为对象的实例。”感谢您的帮助,我现在就知道了。这不仅仅是插入参数,我必须首先检查。比如,如果用户输入“google.com”,我需要将其格式化为“”,或者如果他们输入“www.stackoverflow.com”,我需要将其格式化为“”,如果有办法使用datanaviagteformatstring实现这一点,那么我很乐意知道。好吧,这个网站会自动格式化它,但我需要将http://和www.添加到用户未输入的链接中,以便它成为绝对URL,而不是相对URL。我现在看到了。当用户键入时,您是否修改了它?还是仅用于显示?我保留用户键入的内容并显示,但修改导航URL。
<Columns>
    <asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
    <asp:TemplateField HeaderText="Link"> 
        <ItemTemplate>
            <asp:HyperLink ID="hlParent" runat="server" Text='<% #(Eval("Link")) %>' />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string link = DataBinder.Eval(e.Row.DataItem, "Link") as string;
        if (link != null && link.Length > 0)
        {
            // "FindControl" borrowed directly from DOK
            HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
            if (hlParent != null)
            {
                // Do some manipulation of the link value 
                string newLink = "https://" + link

                // Set the Url of the HyperLink  
                hlParent.NavigateUrl = newLink;
            }
        }
    }
}
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

    }
protected void gvBarcode_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
   }
}