从列中读取信息

时间:2013-12-09 01:38:06

标签: vb.net

我将数据库链接到我的VB.net表单,我想阅读特定列的信息,但找不到任何答案。 DataGrid包含有关人员的信息。我想读取最后一列名称为“Call”,我想将这些值存储到列表中,以便我可以通过特定的函数循环它们。

For Each line As DataGridViewColumn In DataGridView1.Columns
                If Not line.IsNewColumn Then
                    MessageBox.Show(line.Cells(0).Value.ToString & "," & line.Cells(1).Value.ToString)
                End If
            Next

我发现了这个,它看起来像我正在尝试做的但是我不知道如何更改它或将它放入列表中以备任何帮助吗?

4 个答案:

答案 0 :(得分:2)

试试这个。

    Dim colIndex As Integer
    colIndex = 0
    Dim strList As New List(Of String)

    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        strList.Add(DataGridView1.Item(colIndex, i).Value.ToString)
        ListBox1.Items.Add(strList(i))
    Next

Listbox1只是一个测试工具,以便我看看它是否有效,它确实:) strList是您想要的列表,colIndex是DGV中“Call”列的索引

另一种方法就是这样,感谢Plutonix指出了一些重要的细节。

    Dim strList As New List(Of String)

    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        strList.Add(DataGridView1.Item("Call", i).Value.ToString)
    Next
    MsgBox(strList.count) 'number of items in the list.

答案 1 :(得分:1)

我的整个代码如下:我正在尝试从名为“call”的列中获取每个人的数据,以便向他们的手机发送电子邮件。还有文本框,所以你可以发送给1个人而不是整个列表。

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'ContactsDataSet.VBQuery' table. You can move, or remove it, as needed.
    Me.VBQueryTableAdapter.Fill(Me.ContactsDataSet.VBQuery)
    ' Set the caption bar text of the form.   
    Me.Text = "Texting From Email"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try

        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Dim self As New MailAddress("xxx1@gmail.com")
        Dim strCarriers As New List(Of String)
        Dim colIndex As Integer
        Dim strList As New List(Of String)

        colIndex = 0
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("yyy@gmail.com", "blank")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        strCarriers.Add("@pm.sprint.com")
        strCarriers.Add("@vtext.com")
        strCarriers.Add("@tmomail.net")
        strCarriers.Add("@txt.att.net")

        If rad1.Checked = True Then

            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                strList.Add(DataGridView1.Item(colIndex, i).Value.ToString)
                'strList.Items.Add(strList(i))
                'strList.Add(DataGridView1.Item("Call", i).Value.ToString)

                For Each line In strList

                    For Each Carrier As String In strCarriers

                        e_mail = New MailMessage()
                        e_mail.From = self
                        e_mail.To.Add("strlist" + "Carrier")
                        e_mail.Subject = txtSubject.Text
                        e_mail.IsBodyHtml = False
                        e_mail.Body = txtMessage.Text
                        Smtp_Server.Send(e_mail)

                    Next Carrier
                Next
            Next
        ElseIf rad1.Checked = False Then

            For Each Carrier As String In strCarriers

                e_mail = New MailMessage()
                e_mail.From = self
                e_mail.To.Add(txtTo.Text + Carrier)
                e_mail.Subject = txtSubject.Text
                e_mail.IsBodyHtml = False
                e_mail.Body = txtMessage.Text
                Smtp_Server.Send(e_mail)

            Next
        End If

        MsgBox("Mail Sent")

    Catch error_t As Exception
        MsgBox(error_t.ToString)

    End Try
End Sub

答案 2 :(得分:0)

 colIndex = number of the column... Columns.Count - 1 should work if it
 ' really is the last column

 Dim NumToCall As String

 For i As Integer = 0 To DataGridView1.Rows.Count - 1
     ' dont need
     'strList.Add(DataGridView1.Item(colIndex, i).Value.ToString)

     NumToCall = DataGridView1.Item(colIndex, i).Value.ToString 
     ' you cal also reference by name:
     NumToCall = DataGridView1.Item("Call", i).Value.ToString


     ' Send Email
     e_mail.To.Add(NumToCall  & Carrier)

 Next

这也是一个问题:

 For Each Carrier As String In strCarriers

如果收到" Ziggy",它会尝试发送到Ziggy @ pm.sprint.com,Ziggy @ vtext.com,Ziggy @ tvmail.net和Ziggy@txt.att.net。您需要将运营商与每个人相关联以避免这种情况(如果运营商或电子邮件地址不是网格的一部分)。否则,将其分解为GetCarrierSendMail等函数,而不是循环循环中的循环。

......你不应该吃掉像这样的所有例外

答案 3 :(得分:0)

在你的第一个if语句中试试这个。

For i As Integer = 0 To DataGridView1.Rows.Count - 1
    strList.Add(DataGridView1.Item("Call", i).Value.ToString)
Next
For Each str In strList
       For Each Carrier In strCarriers
           e_mail = New MailMessage()
           e_mail.From = self
           e_mail.To.Add(str + Carrier)
           e_mail.Subject = txtSubject.Text
           e_mail.IsBodyHtml = False
           e_mail.Body = txtMessage.Text
           Smtp_Server.Send(e_mail)
       Next
   Next