System.Data.DataRowView在xml中不返回任何值

时间:2021-09-01 16:18:29

I am trying to get different values from two xml elements having same name but different ID here is my code:

我试图从两个具有相同名称但不同ID的xml元素中获取不同的值,这里是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<Vehicle_data>
  <Data ID="1">
    <Vehicle_name>bullet</Vehicle_name>
    <Owner_name>qwe</Owner_name>
    <Number_plate>q2236</Number_plate>
    <Insurance_laps_date>qwe</Insurance_laps_date>
    <Chassis_no>wqe</Chassis_no>
    <Driving_lic_no>q</Driving_lic_no>
    <Entry_date>05-09-15</Entry_date>
    <Entry_time>08:36:52</Entry_time>
  </Data>
  <Data ID="2">
    <Vehicle_name>optra</Vehicle_name>
    <Owner_name>ott</Owner_name>
    <Number_plate>qwerte</Number_plate>
    <Insurance_laps_date>gdsfg</Insurance_laps_date>
    <Chassis_no>dfgdf</Chassis_no>
    <Driving_lic_no>dfgdf</Driving_lic_no>
    <Entry_date>05-09-15</Entry_date>
    <Entry_time>08:38:34</Entry_time>
  </Data>
  <Data ID="3">
    <Vehicle_name>enzo</Vehicle_name>
    <Owner_name>asd</Owner_name>
    <Number_plate>awda</Number_plate>
    <Insurance_laps_date>adaw</Insurance_laps_date>
    <Chassis_no>awdw</Chassis_no>
    <Driving_lic_no>adaw</Driving_lic_no>
    <Entry_date>05-09-15</Entry_date>
    <Entry_time>08:40:26</Entry_time>
  </Data>
  <Data ID="4">
    <Vehicle_name>enzo</Vehicle_name>
    <Owner_name>kok</Owner_name>
    <Number_plate>asdasd</Number_plate>
    <Insurance_laps_date>asfdadsad</Insurance_laps_date>
    <Chassis_no>dasdasd</Chassis_no>
    <Driving_lic_no>asdasdas</Driving_lic_no>
    <Entry_date>05-09-15</Entry_date>
    <Entry_time>08:42:05</Entry_time>
  </Data>
</Vehicle_data>

for this i am using following vb code when form loads

为此我在表单加载时使用以下vb代码

Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\Vehicle_data.xml")

Dim dt As New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("ID")

For Each itm In doc.Descendants("Vehicle_name")
    Dim dr As DataRow = dt.NewRow
    dr("Name") = itm.Value
    dr("ID") = itm.Parent.FirstAttribute.Value
    dt.Rows.Add(dr)
Next

ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"

On ComboBox1.SelectedIndexChanged event:

在ComboBox1.SelectedIndexChanged事件:

Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\Vehicle_data.xml")

If ComboBox1.SelectedItem.ToString <> "System.Data.DataRowView" AndAlso _
    ComboBox1.SelectedValue.ToString <> "System.Data.DataRowView" Then

    Dim vehicledata = From el In doc.Element("Vehicle_data").Elements("Data") _
        Where (el.Element("Vehicle_name") = ComboBox1.SelectedItem.ToString) _
        Select New With 
        {
            .VName = el.Element("Vehicle_name").Value, _
            .onr = el.Element("Owner_name").Value, _
            .nopl = el.Element("Number_plate").Value, _
            .ins = el.Element("Insurance_laps_date").Value, _
            .chassis = el.Element("Chassis_no").Value, _
            .lic = el.Element("Driving_lic_no").Value, _
            .PID = el.Element("Chassis_no").Parent.FirstAttribute.Value
        }

    For Each el In vehicledata
        If ComboBox1.SelectedValue.ToString = el.PID Then
            TextBox5.Text = el.VName
            TextBox1.Text = el.onr
            TextBox2.Text = el.nopl
            TextBox3.Text = el.ins
            TextBox4.Text = el.chassis
            TextBox6.Text = el.lic
        End If
    Next
End If

I think the el.PID is not returning any value. Please help me.

我认为el.PID没有返回任何值。请帮帮我。

1 个解决方案

#1


0  

ComboBox1.SelectedItem.ToString will always equal "System.Data.DataRowView", so it never gets into your if statement. Not sure why you have that if. Is it to stop this code running during the Form_Load? If so, then you should use the SelectionChangeCommitted event instead of SelectedIndexChanged.

ComboBox1.SelectedItem.ToString将始终等于“System.Data.DataRowView”,因此它永远不会进入您的if语句。不知道为什么你有这个。是否在Form_Load期间停止运行此代码?如果是这样,那么您应该使用SelectionChangeCommitted事件而不是SelectedIndexChanged。

Create a new method without that if:

如果出现以下情况,请在没有该方

Private Sub SetControlValues()

    Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\Vehicle_data.xml")

    Dim vehicledata = From el In doc.Element("Vehicle_data").Elements("Data") _
        Where (el.Attribute("ID") = ComboBox1.SelectedValue.ToString) _
        Select New With
        {
            .VName = el.Element("Vehicle_name").Value, _
            .onr = el.Element("Owner_name").Value, _
            .nopl = el.Element("Number_plate").Value, _
            .ins = el.Element("Insurance_laps_date").Value, _
            .chassis = el.Element("Chassis_no").Value, _
            .lic = el.Element("Driving_lic_no").Value, _
            .PID = el.Element("Chassis_no").Parent.FirstAttribute.Value
        }

    For Each el In vehicledata
        If ComboBox1.SelectedValue.ToString = el.PID Then
            TextBox5.Text = el.VName
            TextBox1.Text = el.onr
            TextBox2.Text = el.nopl
            TextBox3.Text = el.ins
            TextBox4.Text = el.chassis
            TextBox6.Text = el.lic
        End If
    Next
End Sub

Note also the updated Where, you need to compare the SelectedValue to the ID attribute.

另请注意更新的Where,您需要将SelectedValue与ID属性进行比较。

Remove the SelectedIndexChanged event and add a new SelectionChangedCommitted event and simply call the above method:

删除SelectedIndexChanged事件并添加一个新的SelectionChangedCommitted事件,只需调用上面的方法:

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    SetControlValues()
End Sub

Finally, to initialise the text boxes during form load, add a call to SetControlValues() as the last line in your Form_Load.

最后,要在表单加载期间初始化文本框,请将对SetControlValues()的调用添加为Form_Load中的最后一行。

#1


0  

ComboBox1.SelectedItem.ToString will always equal "System.Data.DataRowView", so it never gets into your if statement. Not sure why you have that if. Is it to stop this code running during the Form_Load? If so, then you should use the SelectionChangeCommitted event instead of SelectedIndexChanged.

ComboBox1.SelectedItem.ToString将始终等于“System.Data.DataRowView”,因此它永远不会进入您的if语句。不知道为什么你有这个。是否在Form_Load期间停止运行此代码?如果是这样,那么您应该使用SelectionChangeCommitted事件而不是SelectedIndexChanged。

Create a new method without that if:

如果出现以下情况,请在没有该方

Private Sub SetControlValues()

    Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\Vehicle_data.xml")

    Dim vehicledata = From el In doc.Element("Vehicle_data").Elements("Data") _
        Where (el.Attribute("ID") = ComboBox1.SelectedValue.ToString) _
        Select New With
        {
            .VName = el.Element("Vehicle_name").Value, _
            .onr = el.Element("Owner_name").Value, _
            .nopl = el.Element("Number_plate").Value, _
            .ins = el.Element("Insurance_laps_date").Value, _
            .chassis = el.Element("Chassis_no").Value, _
            .lic = el.Element("Driving_lic_no").Value, _
            .PID = el.Element("Chassis_no").Parent.FirstAttribute.Value
        }

    For Each el In vehicledata
        If ComboBox1.SelectedValue.ToString = el.PID Then
            TextBox5.Text = el.VName
            TextBox1.Text = el.onr
            TextBox2.Text = el.nopl
            TextBox3.Text = el.ins
            TextBox4.Text = el.chassis
            TextBox6.Text = el.lic
        End If
    Next
End Sub

Note also the updated Where, you need to compare the SelectedValue to the ID attribute.

另请注意更新的Where,您需要将SelectedValue与ID属性进行比较。

Remove the SelectedIndexChanged event and add a new SelectionChangedCommitted event and simply call the above method:

删除SelectedIndexChanged事件并添加一个新的SelectionChangedCommitted事件,只需调用上面的方法:

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    SetControlValues()
End Sub

Finally, to initialise the text boxes during form load, add a call to SetControlValues() as the last line in your Form_Load.

最后,要在表单加载期间初始化文本框,请将对SetControlValues()的调用添加为Form_Load中的最后一行。