怎样获取DataGridView 在屏幕上显示的行数和列数

怎样获取DataGridView 在屏幕上显示的行数和列数,第1张

ListDsTables(0)RowsCount,这个绝对正确,我的就是用这个

我的代码是:

Dim SqlList As New SystemDataSqlClientSqlCommand

SqlListConnection = cn

SqlListCommandText = "SELECT FROM 发文表 WHERE (条件省略) ORDER BY ID DESC"

Dim ListDa As New SystemDataSqlClientSqlDataAdapter(SqlList)

ListDaSelectCommand = SqlList

Dim ListDs As New DataSet

ListDaFill(ListDs)

CxjgDataSource = ListDsTables(0)DefaultView

CxjgReadOnly = True

CxjgColumns(0)Width = 100

CxjgColumns(1)Width = 100

CxjgColumns(2)Width = 730

CxjgColumns(3)Width = 100

CxjgColumns(4)Width = 100

CxjgTSText = "查询到符合条件的记录 " & ListDsTables(0)RowsCount & " 条。"

其中CxjgTS是一个Label,ListDs是查询出来的数据集,就是datagridview的数据源,Cxjg就是datagridview的名字

那组数据(假定为dataset)

if (dtRowsCount > 0)

{

//绑定到datagridview

datagridview1DataSource=dt;

}

else

{

messageboxshow("");

}

你好,软糖来回答把。

代码已通过测试。

一、获取第一个匹配行

public void 查找特定行() {

    // 用 Cast 将 dataGridView1 的所有行转化为 List<DataGridViewRow>

    var list = dataGridView1RowsCast<DataGridViewRow>();

    // 用 First 返回第一个匹配项符合条件xCells[0]Value 等于 灭火器的 Row

    var firstmatch = listFirst(x => xCells[3]Value != null 

    && xCells[3]ValueToString() == "灭火器"); 

    //firstmatchIndex即为索引,使其高亮显示

        dataGridView1Rows[firstmatchIndex]

        DefaultCellStyleBackColor = ColorYellow;

}

二、获取所有匹配行

// 用 Cast 将 dataGridView 的所有行转化为 List<DataGridViewRow>

var list = dataGridView1RowsCast<DataGridViewRow>();

// 用 Where 返回所有匹配项

var allmatch = listWhere(x => xCells[3]Value != null && xCells[3]ValueToString() == "灭火器");

//RowIndex即为索引,使其高亮显示

foreach (var item in allmatch) {

    dataGridView1Rows[itemIndex]

    DefaultCellStyleBackColor = ColorYellow;

}

对软糖的答案满意吗,请及时采纳,谢谢了。

可以看到datagrid列表,点击超链接时要将收款日期传到下一个页面并显示

首先,要给(单据编号)超链接设置点击方法,在定义datagrid时,我们这样写:

{title:'单据编号', field:'code', width:160, sortable:true,

formatter: function(value,row,index){

if(!value == false) {

return '<a href="#" onclick="getDetailLink('+ rowid +','+index+')" >'+ value +'</a>';

}

}

}

可以看到,getDetailLink()方法就是我们要获取某行数据某字段值处理的方法,需要两个参数 row 的id 和index来帮忙

下面,写getDetailLink()方法的内容:

注意,,此处处理最重要的一点就是,首先要能获取datagrid 的row对象,

即:var row = $('#datagrid')datagrid('getData')rows[index];

之后我们就可以通过类似rowconfirmtime(字段名称)来获取row各个字段(列)的值了。

至此,,完成了获取datagrid某一行(row)各个字段值的 *** 作。

4

最后就可以通过formaction=""来将需要传递到下一个页面的参数加到url里面就ok了

dataGridView1SelectedRows,这个是或得背选中的行

如果你想去出背选中的话,那么可以遍历

foreach (DataGridViewRow dgvr in dataGridView1SelectedRows)

{

//dgvr 就是比背选中的行,遍历,我想你懂的dataGridView1SelectedRows是一个集合,取出每一行当然遍历

//获取或设置

string msg = dgvrCells[0]Value == null "" : dgvrCells[0]ValueToString();

//如果保证烈不为空

string msg2 = dgvrCells[2]ValueToString();

dgvrCells[2]Value=“更改的”;

}

dataGridView1SelectedColumns 是背选中的列,当然取出其中一烈是一样的

如果你要取出所有行的话。那么

foreach (DataGridViewRow dgvr in dataGridView1Rows)

{

//dgvr 就是比被选中的行,遍历,

}

取出列的方式一样

当然还有最常用的,获取所有单元格的值

for (int i = 0; i < dataGridView1RowsCount; i++)

{

for (int j = 0; j < dataGridView1ColumnsCount; j++)

{

if (dataGridView1Rows[i]Cells[j]Value != null)

{

string msg3 = dataGridView1Rows[i]Cells[j]ValueToString();//取出对应的单元格的值

MessageBoxShow(msg3);

}

}

}

只得注意的是,dategridview默认是有一行空值的,当你

dataGridView1Rows[i]Cells[j]Value,这个单元格为空的时候,那么就是null,如果你强制转换为字符串将会异常,所以推介

msg3 = dataGridView1Rows[i]Cells[j]Value==null"空":dataGridView1Rows[i]Cells[j]ValueToString()

获取总行数:dataGridView1RowsCount;

获取当前选中行索引:int i = thisdataGridView1CurrentRowIndex;

获取当前选中列索引:int j = thisdataGridView1CurrentCellColumnIndex

datagridviewCurrentCellRowIndex;是当前活动的单元格的行的索引 datagridviewSelectedRows 是选中行的集合 datagridviewSelectedColumns 是选中列的集合 datagridviewSelectedCells 是选中单元格的集合 DataGridView1CurrentRowIndex 获得包含当前单元格的行的索引 例子: int rows = dataGridView1indexrows;//获得选种行的索引 string str = dataGridView1rows[rows]cells[num]text;//获取第rows行的索引为num列的值

采纳哦

以上就是关于怎样获取DataGridView 在屏幕上显示的行数和列数全部的内容,包括:怎样获取DataGridView 在屏幕上显示的行数和列数、如何获取DataGridView中的有效行数、c#如何获取dataGridView中特定行的行数。 比如 资产名称=灭火器 的行数.等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9720118.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存