如何从gridview中使用linq填充DatakeyValue

时间:2012-09-19 18:26:29

标签: c# linq gridview

我有以下gridview:

                <asp:GridView ID="gvNOVs" runat="server" AllowPaging="true" AllowSorting="true" PageSize="10"
            AutoGenerateColumns="false" Width="100%" BackColor="Transparent" GridLines="None"
            RowStyle-HorizontalAlign="Left" PagerSettings-Mode="NumericFirstLast" DataKeyNames="NOVID"
            OnRowDataBound="gvNOVs_RowDataBound"
            OnRowCommand="gvNOVs_OnSelectRow" 
            OnPageIndexChanging="gvNOVs_PageIndexChanging"
            OnSorting="gvNOVs_OnSorting"
            >
            <AlternatingRowStyle CssClass="alternateItemStyle" />
            <HeaderStyle CssClass="headerStyle" />
            <PagerSettings Mode="NumericFirstLast" />
            <RowStyle CssClass="itemStyle" />

            <Columns>
                <asp:BoundField DataField="NOVID" HeaderText="NOVID" InsertVisible="false" ReadOnly="true" Visible="false"/>
                <asp:BoundField DataField="NOVNumber" HeaderText="NOV Number" SortExpression="NOVNumber"
                    ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="NOVDate" HeaderText="NOV Date" SortExpression="NOVDate"
                    ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
                <asp:BoundField DataField="CompanyName" HeaderText="Company Name" SortExpression="CompanyName"
                    ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                <asp:BoundField DataField="CargoTankID" HeaderText="Cargo Tank Number" SortExpression="CargoTankNumber"
                    ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="ARBInspectorFirstName" HeaderText="InspectorFirstName"
                    SortExpression="InspectorFirstName" ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="ARBInspectorLastName" HeaderText="InspectorLastName" SortExpression="InspectorLastName"
                    ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="ViolationName" HeaderText="Violation Name" SortExpression="ViolationName"
                    ItemStyle-HorizontalAlign="Center" />
            </Columns>
        </asp:GridView>

使用linq:

填充页面加载事件
                var NOVs = from n in db.CT_NOVs
                   join i in db.CT_Inspectors on n.ARBInspectorID equals i.CTInspectorID
                   join v in db.CT_ViolationTypes on n.ViolationTypeID equals v.ViolationTypeID
                   join t in db.CT_Tanks on n.CargoTankID equals t.CargoTankID
                   join c in db.CT_Companies on t.CompanyID equals c.CompanyID
                   orderby n.NOVNumber descending
                   select new
                   {
                       n.NOVID,
                       n.NOVNumber,
                       NOVDate = n.NOVDate.Value.ToShortDateString(),
                       ARBInspectorFirstName = i.FirstName,
                       ARBInspectorLastName = i.LastName,
                       v.ViolationName,
                       t.CargoTankID, 
                       c.CompanyName
                   };

        this.gvNOVs.DataSource = NOVs;
        this.gvNOVs.DataBind();

当我点击一行时,我称之为:

protected void gvNOVs_OnSelectRow(object sender, GridViewCommandEventArgs e)
{


}

我不知道如何获取datakeyValue,我想获取被点击的行的NOVID。如果我得到e.CommandArgument.ToString(),它会给我CargoTankID的值,我不知道为什么它给了我这个值。

有人知道如何获取所选行的NOVID吗?

2 个答案:

答案 0 :(得分:0)

这可能有用。

  1. CommandArgument
  2. 获取索引
  3. CommandSource
  4. 获取网格本身
  5. 获取数据源
  6. 获取索引

    的数据项

    int index = int.Parse((string)e.CommandArgument); var grid =(GridView)e.CommandSource; var data =(IList)grid.DataSource; var NOVID = data [index] .NOVID;

答案 1 :(得分:0)

你应该尝试这样的事情

string dataKeyValue = (string)this.gvNOVs.SelectedDataKey.Value;