在 wpf 中使用 DataGridView(winForms)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26938047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:42:03  来源:igfitidea点击:

using DataGridView(winForms) in wpf

c#wpfwinformsxamldatagridview

提问by cmoha

I am using datagridview (not datagrid) in my wpf application and i use this code to call it in my Window

我在 wpf 应用程序中使用 datagridview(不是 datagrid),我使用此代码在我的窗口中调用它

<Window x:Class="TestHosting.MainWindow"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="440.132" Width="521.053">
<Grid>

    <WindowsFormsHost Margin="20,20,130,142">
        <wf:DataGridView x:Name="dgv">

            <wf:DataGridView.Columns>
                <wf:DataGridViewColumn Name="column1" HeaderText="Col l" />
                <wf:DataGridViewColumn Name="column2" HeaderText="Col 2"/>
                <wf:DataGridViewColumn Name="column3" HeaderText="Col 3"/>
            </wf:DataGridView.Columns>

        </wf:DataGridView>
    </WindowsFormsHost>

and the problem is that an error says that "At Least on of the datagridview controls' columns has no cell template."

问题是一个错误说“至少在 datagridview 控件的列中没有单元格模板。”

how should I fix this error ?

我应该如何解决这个错误?

回答by Edwards Moses

You could use DataGrid, instead of DataGridView

您可以使用DataGrid,而不是 DataGridView

SqlDataAdapter da = new SqlDataAdapter("Select * from Table", con);
                DataTable dt = new DataTable("Call Reciept");
                da.Fill(dt);
                DataGrid dg = new DataGrid();
                dg.ItemsSource = dt.DefaultView;