本文介绍了如何在数据网格中添加sr no的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想在数据网格中添加一个没有列的列如何为下面的代码添加 私有 Sub Button_Click_1(发件人 As 对象,e As RoutedEventArgs) Dim dt 作为 新 DataTable() Dim SearchCriteria = cmbSearch.Text dt.Columns.Add( 作者, GetType ( String )) dt.Columns.Add ( 标题, GetType ( String )) dt.Columns.Add( AccessionNo, GetType (字符串)) dt.Columns.Add( 位置, GetType ( String )) dt.Columns.Add( 状态, GetType (字符串)) Dim xmldoc 作为 New XmlDocument() xmldoc.Load( C:\ Users \Shahrukh \Documents\Visual Studio 2012 \Projects \Simple search1 \Simple search1 \New folder \ Data.xml) Dim nodeList As XmlNodeList = xmldoc.SelectNodes( / NewDataSet / Table) 如果 SearchCriteria = 作者 然后 对于 每个节点 As XmlNode 在 nodeList Dim dtrow As DataRow = dt.NewRow() 如果( LCase(节点( 作者)。InnerText).Contains(LCase(txtSearch.Text()) ))然后 dtrow( Title )= node( title)。InnerText ' 已修复 dtrow( 作者) = node( 作者)。InnerText dtrow( AccessionNo)= node( AccessionNo)。InnerText dtrow( 位置) = node( location)。InnerText ' fixed dtrow( Status )= node( status)。InnerText ' fixed dt.Rows.Add(dtrow) 结束 如果 下一步 结束 如果 dGridResults.ItemsSource = dt.DefaultView 结束 Sub 解决方案 试试这个。未经测试。 私有 Sub Button_Click_1 (发件人作为 对象,e 作为 RoutedEventArgs ) Dim dt As 新 DataTable() Dim SearchCriteria = cmbSearch.Text dt.Columns.Add( SrNo, GetType (int)) dt.Columns .Add( 作者, GetType ( String )) dt.Columns.Add( 标题, GetType ( String )) dt.Columns.Add( AccessionNo, GetType ( String )) dt.Columns.Add ( 位置, GetType ( String )) dt.Columns.Add( 状态, GetType (字符串)) Dim xmldoc 作为 新 XmlDocument() xmldoc。加载( C:\ Users \Shahrukh \ Files \ Visual Studio 2012 \Projects \Simple search1 \Simple search1 \New folder\Data.xml) Dim nodeList As XmlNodeList = xmldoc.SelectNodes( / NewDataSet / Table) 如果 SearchCriteria = 作者 然后 Dim SrNo As Integer = 1; 对于 每个节点作为 XmlNode 在 nodeList Dim dtrow As DataRow = dt.NewRow() 如果(LCase(节点( 作者)。InnerText).Contains(LCase(txtSearch.Text())))然后 dtrow( SrNo)= SrNo dtrow( 标题)= node( title)。InnerText ' fixed dtrow( 作者)= node( 作者 ).InnerText dtrow( AccessionNo)= node( AccessionNo)。InnerText dtrow( 位置)= node( location ).InnerText ' fixed dtrow( 状态)= node( status )。InnerText ' fixed dt.Rows.Add(dtrow) SrNo = SrNo + 1 结束 如果 下一步 结束 如果 dGridResults.ItemsSource = dt.DefaultView 结束 Sub I want to add a sr no column in data grid how can i do for below code Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Dim dt As New DataTable() Dim SearchCriteria = cmbSearch.Text dt.Columns.Add("Author", GetType(String)) dt.Columns.Add("Title", GetType(String)) dt.Columns.Add("AccessionNo", GetType(String)) dt.Columns.Add("Location", GetType(String)) dt.Columns.Add("Status", GetType(String)) Dim xmldoc As New XmlDocument() xmldoc.Load("C:\Users\Shahrukh\Documents\Visual Studio 2012\Projects\Simple search1\Simple search1\New folder\Data.xml") Dim nodeList As XmlNodeList = xmldoc.SelectNodes("/NewDataSet/Table") If SearchCriteria = "Author" Then For Each node As XmlNode In nodeList Dim dtrow As DataRow = dt.NewRow() If (LCase(node("Author").InnerText).Contains(LCase(txtSearch.Text()))) Then dtrow("Title") = node("title").InnerText ' fixed dtrow("Author") = node("Author").InnerText dtrow("AccessionNo") = node("AccessionNo").InnerText dtrow("Location") = node("location").InnerText ' fixed dtrow("Status") = node("status").InnerText ' fixed dt.Rows.Add(dtrow) End If Next End If dGridResults.ItemsSource = dt.DefaultView End Sub 解决方案 Try this one. Not tested.Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Dim dt As New DataTable() Dim SearchCriteria = cmbSearch.Text dt.Columns.Add("SrNo", GetType(int)) dt.Columns.Add("Author", GetType(String)) dt.Columns.Add("Title", GetType(String)) dt.Columns.Add("AccessionNo", GetType(String)) dt.Columns.Add("Location", GetType(String)) dt.Columns.Add("Status", GetType(String)) Dim xmldoc As New XmlDocument() xmldoc.Load("C:\Users\Shahrukh\Documents\Visual Studio 2012\Projects\Simple search1\Simple search1\New folder\Data.xml") Dim nodeList As XmlNodeList = xmldoc.SelectNodes("/NewDataSet/Table") If SearchCriteria = "Author" Then Dim SrNo As Integer = 1; For Each node As XmlNode In nodeList Dim dtrow As DataRow = dt.NewRow() If (LCase(node("Author").InnerText).Contains(LCase(txtSearch.Text()))) Then dtrow("SrNo") = SrNo dtrow("Title") = node("title").InnerText ' fixed dtrow("Author") = node("Author").InnerText dtrow("AccessionNo") = node("AccessionNo").InnerText dtrow("Location") = node("location").InnerText ' fixed dtrow("Status") = node("status").InnerText ' fixed dt.Rows.Add(dtrow) SrNo = SrNo + 1 End If Next End If dGridResults.ItemsSource = dt.DefaultView End Sub 这篇关于如何在数据网格中添加sr no的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-24 17:51