如何编辑绑定到数据网格的数据?

时间:2009-12-31 15:34:31

标签: c# asp.net datagrid

参考Link loaded into my gridview try to navigate to my local server。我在数据网格中的列是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>

3 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望获得名为 Link 的数据项的值。如果是这样,那么这样的事情应该有效:

编辑:我认为你所说的是你想要从数据库中获取值Link,对其进行操作然后设置HyperLink的网址对于新的,被操纵的值,如果是这样,那么它将如下所示:

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

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

我通过添加ID并从NavigateUrl控件中删除对HyperLink属性的引用来修改原始问题中的ASP。

<强>代码

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

答案 1 :(得分:1)

为GridView中的每一行调用RowDataBound,包括页眉,页脚等。因此,您应该首先检查包含数据的行。

一旦你连续,有几种方法来检查细胞。一种是使用单元格索引(此处为2)。在你的情况下这似乎很简单,但是如果你重新排列列,会导致沮丧。

以下是来自MSDN

的示例
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>";

    }

更好的方法是将FindControl与项目的ID一起使用。

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

答案 2 :(得分:0)

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

如果您正在尝试这样做,可以使用datanavigateurlformatstring属性插入查询字符串参数。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring.aspx