2016-05-12 2 views
0

私はgridviewを持っていて、その行datakeynameにASP.NET/C#のイベントondatabindingからアクセスする必要があります。 ondatabindingからデータキーにどのようにアクセスすればよいですか?これは3つの異なるグリッドビューのうちの1つによって呼び出すこともできます。どのように私はこの行datakeynameをondatabindingから得るでしょうか?ondatabindingイベントからどのようにデータキーを取得できますか?

ondatabinding = "PreventErrorOnbinding"

DataKeyNames="ID" 

<asp:TemplateField HeaderText="Purpose" SortExpression="Purpose" ItemStyle-Width="50px" HeaderStyle-Width="50px" HeaderStyle-Wrap="false"> 
         <ItemTemplate> 
          <asp:Label ID="LabelPurpose" runat="server" Text='<%# Eval("Purpose") %>'></asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:DropDownList ID="ddPurpose1" ondatabinding="PreventErrorOnbinding" class="txtSize" SelectedValue='<%# Bind("Purpose") %>' AppendDataBoundItems="true" runat="server"> 
          </asp:DropDownList> 
          <asp:RequiredFieldValidator ID="ddPurposeRequiredFieldValidator1" runat="server" Display="Dynamic" ForeColor="Red" 
          ErrorMessage="Select a purpose type." InitialValue="" ControlToValidate="ddPurpose1" ValidationGroup="relationshipGroup2" />        
         </EditItemTemplate> 

のC#:

protected void PreventErrorOnbinding(object sender, EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)sender; 
    ddl.DataBinding -= new EventHandler(PreventErrorOnbinding); 
    ddl.AppendDataBoundItems = true; 
    ListItem li = new ListItem("Select a purpose type.", ""); 
    ddl.Items.Insert(0, li); 

    //Get this purpose from the db and insert it into the list and then add the other default items also. 
    //This will allow us to edit items that do not exist within the list that were set before this was a dropdown list when it was a textbox. 
    try 
    { 
     ddl.DataBind(); 
    } 
    catch (ArgumentOutOfRangeException) 
    { 
     GridViewRow row = (GridViewRow)ddl.NamingContainer; 
     //int index = GetColumnIndexByName(row, "ID"); 
     //Int64 columnValue = Convert.ToInt64(row.Cells[index].Text.Trim()); 

     //similar to this but the gridview would need to be accessed dynamically. 
     // var id = gvTransactionHistory.DataKeys[rowIndex].Values["ID"]; 

     int RowIndex = row.RowIndex; 
     //Int64 rowID = Convert.ToInt64(????.DataKeys[RowIndex].Values["ID"].ToString().Trim()); 

     //ddl.SelectedValue = Globals.GetDropDownValue(rowID); 
     //get item from the db for this contact 
     //append the rest 
     ddl.Items.Clear(); 
     ddl.DataSourceID = ddPurposeSqlDataSource.ID; 
     ddl.DataBind(); 
    } 
} 
+0

ありがとうございました! GridViewRow row =(GridViewRow)ddl.NamingContainer; DataRow dr =((DataRowView)row.DataItem).Row; string id = dr ["ID"]。ToString()。Trim();string id2 =((System.Data.DataRowView)(row.DataItem))行["ID"]。ToString()。Trim(); – cpeterson

答えて

0

はそれを手に入れました!

 GridViewRow row = (GridViewRow)ddl.NamingContainer; 
     DataRow dr = ((DataRowView)row.DataItem).Row; 
     string id = dr["ID"].ToString().Trim(); 
     string id2 = ((System.Data.DataRowView)(row.DataItem)).Row["ID"].ToString().Trim(); 
関連する問題