如何在VB.NET中的datagridview中显示特定单元格的数量 [英] How can I display the number of specific cell in a datagridview in VB.NET

查看:150
本文介绍了如何在VB.NET中的datagridview中显示特定单元格的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,我通过访问数据库中的文本框过滤学生的成绩。

当我输入A或B或C或F等级时,它的工作原理如下... .it显示具有该特定年级的学生。

我想要的是显示DataGridView按钮列中每个学生的成绩总数或其他一些显示方法。



谢谢!!!



这是加载事件和我已经完成的文本框文本更改事件。



i have a datagridview that i filter the grades of students via a text box from access database.
It works like this when i type some grade like A or B or C Or F....it displays the students with that specific grade.
What i want is to display the total number of grade of each students inside a DataGridView Button Column or some other displaying method.

Thank you!!!

Here is the load event and the text box text change event that i have deone.

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable







Public Class Form2
    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
    Dim ds As DataSet = New DataSet
    Dim da As OleDbDataAdapter
    Dim tables As DataTableCollection = ds.Tables
    Dim source1 As New BindingSource()


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        ' dataFile = "|DataDirectory|\LMS_DB.accdb"
        dataFile = "C:\Studentsgrade.accdb" ' change to access database location on your computer
        connString = provider & dataFile
        myConnection.ConnectionString = connString
        da = New OleDbDataAdapter("Select [Student Name], [ID No], [Group], [C1], [C2], [C3], [C4], [C5], [C6], [C7], [C8], [C9], [C10], [C11], [C12], [C14], [C15], [C3], [C1], [C2], [C3], [C1], [C2], [C3], [C1], [C2], [C3], [C1], [C2], [C3] from Studentsgrade", myConnection)
        da.Fill(ds, "Studentsgrade")
        ' replace "items" with the name of the table
        ' replace [Item Code], [Description], [Price] with the columns headers
        Dim view1 As New DataView(tables(0))
        source1.DataSource = view1
        DataGridView1.DataSource = view1
        DataGridView1.Refresh()

    End Sub
    
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
       
        source1.Filter = String.Format("[Student Name] LIKE '%{0}%' OR [ID No] LIKE '%{0}%' OR [Group] LIKE '%{0}%' OR [C1] LIKE '%{0}%' OR [C2] LIKE '%{0}%'", TextBox1.Text)
        
        DataGridView1.Refresh()
End Sub





我尝试过:



i已尝试加载事件和文本框文本更改事件的代码。



What I have tried:

i have tried the code for the load event and the text box text change event.

推荐答案

有两种可能的方法来实现:

There's 2 possible ways to achieve that:


  1. 通过过滤数据库

    您需要创建OleDbCommand [ ^ ]:


  1. via filter database
    You need to create OleDbCommand[^] with query:
SELECT <<list_of_columns>>
FROM Studentsgrade
WHERE [Student Name] LIKE @name OR [ID No] LIKE @id OR [Group] LIKE @grp OR [C1] LIKE @c1 OR [C2] LIKE @c2



并将您在 where 子句中定义的参数数量传递给 OleDbCommand.Parameters [ ^ ]集合。

能够将数据加载到DataTable [ ^ ]你必须使用 OleDbReader [ ^ ]通过 OleDbCommand.ExecuteReader()方法 [ ^ ]。br $>

  • 通过过滤器数据集

    对于MS Access数据库,一些程序员更喜欢使用其方法过滤数据表 [ ^ ]有些人喜欢使用 Linq to dataset [ ^ ]。


  • and pass the number of arguments you define in where clause to OleDbCommand.Parameters[^] collection.
    To be able to load data to DataTable[^] you have to use OleDbReader[^] through the OleDbCommand.ExecuteReader() method[^].

  • via filter dataset
    For MS Access database some programmers prefer to filter datatable by using its method[^] and some prefer to use Linq to dataset[^].






  • 详情请参阅:

    OleDbParameterCollection.AddWithValue Method(String,Object)(System.Data.OleDb) [ ^ ]

    Visual Basic中的LINQ简介 [ ^ ]

    LINQ to DataSet示例 [ ^ ]

    从查询(LINQ到DataSet)创建DataTable [ ^ ]

    LINQ to DataSet中的查询 [ ^ ]




    For further details, please read:
    OleDbParameterCollection.AddWithValue Method (String, Object) (System.Data.OleDb)[^]
    Introduction to LINQ in Visual Basic[^]
    LINQ to DataSet Examples[^]
    Creating a DataTable From a Query (LINQ to DataSet)[^]
    Queries in LINQ to DataSet[^]


    这篇关于如何在VB.NET中的datagridview中显示特定单元格的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆