小编Roh*_*hit的帖子

使用Linq连续选择多个元素

我的守则如下

var users = MyTable.AsEnumerable()
                      .Select(x => new { x.Field<string>("Col1"),x.Field<string>  
                       ("Col2")}).ToList();
Run Code Online (Sandbox Code Playgroud)

在编译我得到

无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问声明匿名类型成员.

.net c# linq

10
推荐指数
1
解决办法
1万
查看次数

在WPF C#TreeView中获取子节点的父节点

我知道使用WPF在C#中编程与传统的C#程序不同,因此大多数在线资料都没有说明我需要的内容.

我的WPF窗口中有一个TreeView控件,其中有父节点和子节点.我想将它们存储在Node类型的列表中(id,name,parent).

在此输入图像描述

我使用这个得到了所选项目/节点的名称:

private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
    TreeViewItem item = treeView.SelectedItem as TreeViewItem;
    nameTxt.Text = item.Header.ToString();
}
Run Code Online (Sandbox Code Playgroud)

我尝试在使用它之前立即获取子节点的Parent:

TreeViewItem item = treeView.SelectedItem as TreeViewItem;
nameTxt.Text = item.Parent.ToString();
Run Code Online (Sandbox Code Playgroud)

但是,这将返回根父(A)而不是子父(即2).

我应该做些什么来改变孩子的直接父母而不是根父母?:)

编辑:这是XAML

<TreeView Name="treeView" HorizontalAlignment="Left" Height="564" Margin="10,68,0,0" VerticalAlignment="Top" Width="363">
     <TreeViewItem TreeViewItem.Selected="TreeViewItem_OnItemSelected"  Header="A" IsExpanded="True" Height="554" FontSize="18">
                <TreeViewItem Header="1" />
                <TreeViewItem Header="2" />
     </TreeViewItem>
</TreeView>
Run Code Online (Sandbox Code Playgroud)

.net c# wpf treeview

9
推荐指数
1
解决办法
2万
查看次数

如何在int数组中分配内存

int数组占用多少空间?或者int数组消耗的空间(以字节为单位)看起来像这样:

 int[] SampleArray=new int[]{1,2,3,4};
Run Code Online (Sandbox Code Playgroud)

内存分配语言是否具体?

谢谢你们

c# java arrays oop

5
推荐指数
1
解决办法
2662
查看次数

使用 C# 将光标移动到 MsWord 中文本的末尾?

这个问题听起来可能很简单,但我无法找到任何解决方案。我想做的是将MsWord中的光标位置移动到文本末尾。我的代码如下

  object StartPos = 0;
  object Endpos = 1;
  Microsoft.Office.Interop.Word.Range rng= oDoc.Range(ref StartPos, ref Endpos);
  rng.Text = "This is first line Word from C#";
Run Code Online (Sandbox Code Playgroud)

输出是

I这是 C# 中的第一行 Word

不过我想要这样的东西

这是 C# I中的第一行 Word

谢谢大家

c# ms-word visual-studio

4
推荐指数
1
解决办法
1万
查看次数

从通用列表<Datarow>到通用列表<string>的转换出错

我有下面的代码

List<string> esfa = NewTable.AsEnumerable().Where(row => row.Field<string>("Select")   
=="true").ToList();
Run Code Online (Sandbox Code Playgroud)

编译时收到错误

无法隐式转换'System.Collections.Generic.List<System.Data.Datarow>''System.Collections.Generic.List<string>'

请帮忙.

.net c# linq compiler-errors

3
推荐指数
1
解决办法
1435
查看次数

如何检查分配了哪些事件?

我的代码如下。

Control[] FoundControls = null;
FoundControls = MyFunctionToFilter(TF, c => c.Name != null && c.Name.StartsWith("grid"));
var eventinfo = FoundControls[0].GetType().GetEvents();
Run Code Online (Sandbox Code Playgroud)

但是,eventinfo 为我提供了属于网格的所有控件的列表。而主类中只定义了两个事件:KeyDownValidating 。

如何获取这些指定事件的列表,即 Keydown 和 Validating?

c# reflection controls winforms

3
推荐指数
1
解决办法
3283
查看次数

ASP.NET Web 应用程序中的 CAS 策略错误

我在我的 WEB 表单中使用 Devexpress 控件。我在添加控件时添加了对我的 refrences 文件夹的相应引用(我在这里使用网格)给了我错误

此方法显式使用 CAS 策略,该策略已被 .NET Framework 废弃。为了出于兼容性原因启用 CAS 策略,请使用 NetFx40_LegacySecurityPolicy 配置开关。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=155570

我检查了解决方案

并按照链接中的说明更改了我的网络配置

但是我遇到了同样的错误,我使用的是 VS 2010,我的 .NET 框架是 4,我运行的是 32 位 OS XP

请帮忙

c# asp.net cas webforms

3
推荐指数
1
解决办法
1万
查看次数

如何处置我的 Excel 应用程序

我的代码如下

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file);

Excel.Worksheet xlSheet = xlWorkbook.Sheets[1]; // get first sheet
Excel.Range xlRange = xlSheet.UsedRange;
Run Code Online (Sandbox Code Playgroud)

这些是我的函数中使用的唯一变量

foreach (Excel.Worksheet XLws in xlWorkbook.Worksheets)
{
    // do some stuff 

    xlApp.UserControl = false;

    if (xlRange != null)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlRange);

    if (xlSheet != null)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlSheet);

    if (xlWorkbook != null)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkbook);

    xlRange = null;
    xlSheet = null;
    xlWorkbook = null;
    xlApp.Quit();

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp);
}
Run Code Online (Sandbox Code Playgroud)

但我仍然在任务管理器中得到EXCEL.EXE

请帮忙?

c# excel automation

3
推荐指数
2
解决办法
1万
查看次数

如何将对象列表绑定到数据网格?

首先,我阅读了所有更有趣的问题并尝试了他们的答案。似乎没有什么对我有用。

我创建了一个名为 的类student。然后我从数据库加载了学生列表。

现在,我希望在数据网格中显示此列表。我得到的只是一张空桌子..:(

似乎有什么问题?(我尝试使用 datacontext 而不是 itemSource)

我的 C# 代码:

public partial class MainWindow : Window
{
    public List<student> studentsList = new List<student>();
    public MainWindow()
    {
        try
        {
            InitializeComponent();
            Classes.studentManager stdManager = new Classes.studentManager();


            studentsList = stdManager.Select();

            this.dgStudents.DataContext = studentsList.ToList();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML代码如下

 <DataGrid AutoGenerateColumns="False" Height="211" Name="dgStudents" Width="Auto" Margin="39,0,-33,-142" VerticalAlignment="Bottom" ItemsSource="{Binding}">
   <DataGrid.Columns>
     <DataGridTextColumn Header="birthDate" Width="175" Binding="{BindingfirstName}" />
    </DataGrid.Columns>
   </DataGrid>
Run Code Online (Sandbox Code Playgroud)

这是学生对象

  public class student
  {

    public int ID;
    public string firstName; …
Run Code Online (Sandbox Code Playgroud)

c# wpf

3
推荐指数
1
解决办法
728
查看次数

如何使用C#..读取/加载datagridview中的文本(* .txt)文件值?

谁能帮我..?

在这里,我需要在datagridview中读取/加载文本(* .txt)文件值。这是该示例文本文件,我需要加载。

 S.NO   Data1  Data2    Data3   Data4   Data5  Data6   Data7   Data8   Data9   Data10

 1      8.3     2       9.1     3       7.5     1       25      1.5     22      1.7 
 2      5.6     4       8.2     6       8.6     3       26      2.5     23      2.3 
 3      8.7     6       7.3     9       9.3     5       28      3.5     26      3.7 
 4      2.9     8       6.4     12      4.9     7       12      4.5     24      4.3 
 5      4.6    10       5.5     15      5.7     9       25      5.5     25      5.3
Run Code Online (Sandbox Code Playgroud)

谁能提出如何将这些文本文件值加载到我的datagridview单元中的信息,诸如data1,data2,..... data10之类的标题必须加载到列标题中,而其余的行值应根据其列标题加载到datagridview的单独单元中。

提前致谢。

c# datagridview winforms

3
推荐指数
1
解决办法
3万
查看次数

无法初始化类型'int'错误

我在C#中有一个简单的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyProject
{
  public static class Class1
  {
    public static int[] Iparray = new int { 12, 9, 4, 99, 120, 1, 3, 10 };
  }
}
Run Code Online (Sandbox Code Playgroud)

但是在(Ctrl+ Shift+ B)上显示的错误是

Cannot initialize type 'int' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

我正在使用vs 2010和.NET framework 4

谢谢你们

c# console-application visual-studio-2010

2
推荐指数
1
解决办法
1874
查看次数

如何附加我的列表元素

我有一个以下的字符串列表

          List<string> myList=new List<string>();
           myList.Add("A");
           myList.Add("B");
           myList.Add("C");
           myList.Add("D");
           myList.Add("E");
           myList.Add("F");
           myList.Add("G");
           myList.Add("H");

           string Res=""
           foreach(String str in myList)
           {
               Res+=","+str;
            }
Run Code Online (Sandbox Code Playgroud)

有没有比这更好的方法来加入我的List值?

谢谢你们

c# linq string

1
推荐指数
1
解决办法
498
查看次数

不能从基类继承

我的代码如下

class BaseClass<T> where T : class
{
    class DerivedClass<U, V>
        where U : class
        where V : U
    {
        BaseClass<V> _base;
    }

}
Run Code Online (Sandbox Code Playgroud)

错误:类型"V"必须是引用类型.

这里的类型类不是'V'吗?

c# inheritance

0
推荐指数
1
解决办法
88
查看次数