如何将值从Datagridview选定行复制到文本框

时间:2016-09-04 08:13:42

标签: c# datagridview

我正在尝试选择datagridview行并将值复制到3个文本框。

我尝试了这个代码(它在另一个我的项目中工作),但是我收到了一个错误。这是代码:

Error 1 ' bexscraping.Form1 ' does not contain a definition of ' DataGridView2 ' and has not been found no extension method ' DataGridView2 ' accepting a first argument of type ' bexscraping.Form1 ' . Probably missing a using directive or a reference to an assembly.

这是我的错误:

<?xml version="1.0" encoding="Windows-1252"?><SelfBilledInvoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:SelfBilledInvoice-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:ccts="urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2" xmlns:stat="urn:oasis:names:specification:ubl:schema:xsd:DocumentStatusCode-1.0" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:udt="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2">
  <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
  <cbc:ID>CBF-123</cbc:ID>
  <cbc:CopyIndicator>false</cbc:CopyIndicator>
  <cbc:IssueDate>2016-07-05</cbc:IssueDate>
  <cbc:InvoiceTypeCode>D</cbc:InvoiceTypeCode>
  <cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
  <cbc:LineCountNumeric>1</cbc:LineCountNumeric>
  <cac:BillingReference>
    <cac:SelfBilledInvoiceDocumentReference>
      <cbc:ID>112.pdf</cbc:ID>
    </cac:SelfBilledInvoiceDocumentReference>
  </cac:BillingReference>
  <cac:AccountingCustomerParty>
    <cac:Party>
      <cac:PartyIdentification>
        <cbc:ID>30276460</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>CB</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:Postbox>123</cbc:Postbox>
        <cbc:CityName>City</cbc:CityName>
        <cbc:PostalZone>zipcode</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>NL</cbc:IdentificationCode>
          <cbc:Name>Nederland</cbc:Name>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID>NL123</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:Name>NL VAT</cbc:Name>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
    </cac:Party>
    <cac:AccountingContact>
      <cbc:Telephone>0111-1118</cbc:Telephone>
      <cbc:Telefax>0111-11118</cbc:Telefax>
    </cac:AccountingContact>
  </cac:AccountingCustomerParty>
  <cac:AccountingSupplierParty>
    <cac:Party>
      <cac:PartyIdentification>
        <cbc:ID>9573703</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>A&E</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:StreetName>Street 33</cbc:StreetName>
        <cbc:InhouseMail>205</cbc:InhouseMail>
        <cbc:CityName>City</cbc:CityName>
        <cbc:PostalZone>zone</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>NL</cbc:IdentificationCode>
          <cbc:Name>Nederland</cbc:Name>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID>NL222</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:Name>NL VAT</cbc:Name>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
    </cac:Party>
  </cac:AccountingSupplierParty>
  <cac:PaymentMeans>
    <cbc:PaymentMeansCode>AO</cbc:PaymentMeansCode>
    <cbc:PaymentDueDate>2016-09-15</cbc:PaymentDueDate>
    <cbc:InstructionNote>Totaal af te dragen omzet</cbc:InstructionNote>
    <cac:PayerFinancialAccount>
      <cbc:ID>NL11RAB11111111</cbc:ID>
      <cac:FinancialInstitutionBranch>
        <cbc:ID>RABONL1U</cbc:ID>
        <cbc:Name>Rabobank</cbc:Name>
      </cac:FinancialInstitutionBranch>
    </cac:PayerFinancialAccount>
  </cac:PaymentMeans>
  <cac:TaxTotal>
    <cbc:TaxAmount currencyID="EUR">0.66</cbc:TaxAmount>
    <cac:TaxSubtotal>
      <cbc:TaxableAmount currencyID="EUR">10.94</cbc:TaxableAmount>
      <cbc:TaxAmount currencyID="EUR">0.66</cbc:TaxAmount>
      <cbc:Percent>6</cbc:Percent>
      <cac:TaxCategory>
        <cac:TaxScheme>
          <cbc:Name>NL VAT</cbc:Name>
        </cac:TaxScheme>
      </cac:TaxCategory>
    </cac:TaxSubtotal>
  </cac:TaxTotal>
  <cac:LegalMonetaryTotal>
    <cbc:TaxExclusiveAmount currencyID="EUR">10.94</cbc:TaxExclusiveAmount>
    <cbc:PayableAmount currencyID="EUR">11.60</cbc:PayableAmount>
  </cac:LegalMonetaryTotal>
  <cac:InvoiceLine>
    <cbc:ID>79469458</cbc:ID>
    <cbc:InvoicedQuantity>1</cbc:InvoicedQuantity>
    <cbc:LineExtensionAmount currencyID="EUR">10.94</cbc:LineExtensionAmount>
    <cac:OrderLineReference>
      <cbc:LineID>82947243</cbc:LineID>
    </cac:OrderLineReference>
    <cac:AllowanceCharge>
      <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
      <cbc:MultiplierFactorNumeric>0.42</cbc:MultiplierFactorNumeric>
      <cbc:Amount currencyID="EUR">7.92</cbc:Amount>
    </cac:AllowanceCharge>
    <cac:TaxTotal>
      <cbc:TaxAmount currencyID="EUR">0.66</cbc:TaxAmount>
      <cac:TaxSubtotal>
        <cbc:TaxableAmount currencyID="EUR">10.94</cbc:TaxableAmount>
        <cbc:TaxAmount currencyID="EUR">0.66</cbc:TaxAmount>
        <cbc:Percent>6</cbc:Percent>
        <cbc:PerUnitAmount currencyID="EUR">18.86</cbc:PerUnitAmount>
        <cac:TaxCategory>
          <cac:TaxScheme>
            <cbc:Name>NL VAT</cbc:Name>
          </cac:TaxScheme>
        </cac:TaxCategory>
      </cac:TaxSubtotal>
    </cac:TaxTotal>
    <cac:Item>
      <cbc:Description>TITLE</cbc:Description>
      <cac:StandardItemIdentification>
        <cbc:ID>9789079995318</cbc:ID>
      </cac:StandardItemIdentification>
      <cac:AdditionalItemProperty>
        <cbc:Name>Afnemer</cbc:Name>
        <cbc:Value>ABC</cbc:Value>
      </cac:AdditionalItemProperty>
    </cac:Item>
    <cac:Price>
      <cbc:PriceAmount currencyID="EUR">19.99</cbc:PriceAmount>
    </cac:Price>
  </cac:InvoiceLine>
</SelfBilledInvoice>

我不知道错误在哪里 - 任何想法?

编辑:我忘了初始化我的datagridview; M现在我的代码正常运行!无论如何,谢谢!

3 个答案:

答案 0 :(得分:0)

确保初始化gridView。然后尝试这个

private void dataGridView1_Click(object sender, EventArgs e)
    {
        if(dataGridView1.SelectedRows.Count>0){
           textBox1.Text = dataGridView1.SelectedRows[0].Cells["HomeNM"].Value.ToString();
           textBox2.Text = dataGridView1.SelectedRows[0].Cells["HostNM"].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells["odd1NM"].Value.ToString();
    }
   } // haha

答案 1 :(得分:0)

首先确保名称DataGridView2是正确的。

然后打开设计器,单击DataGridView2并在属性中确保Design - &gt; GenerateMember设置为true。

这有帮助吗?

答案 2 :(得分:0)

先生试试这个。

if (e.RowIndex >= 0)
{
 txtbox1.text = datagridview1.currentrow.cells["cellname"].value
 . . . 'and so on.
}

希望这能解决您的错误