当前位置:编程学习 > C#/ASP.NET >>

datagridview如何添加固定行和列?

   刚学C#,请问如何用代码实现datagridview添加固定行和列,如下图片,谢谢了。 DataGridView --------------------编程问答-------------------- 表格里面不加数据么??? --------------------编程问答-------------------- 前台.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="251" HorizontalAlignment="Left" Margin="59,24,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="401" CanUserSortColumns="False" CanUserAddRows="False" Grid.Column="1" Grid.Row="10">
            <DataGrid.Columns>

                <DataGridTextColumn IsReadOnly="true" Header="" Width="*" Binding="{Binding count}" />
                <DataGridTextColumn IsReadOnly="true" Header="日期" Width="*"/>
                <DataGridTextColumn IsReadOnly="true" Header="摘要" Width="*" />
                <DataGridTextColumn IsReadOnly="true" Header="餐费" Width="*"/>
                <DataGridTextColumn IsReadOnly="true" Header="招待费" Width="*"/>
                <DataGridTextColumn IsReadOnly="true" Header="福利费" Width="*" />
                <!--剩下的照着写吧-->
           
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

后台.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DataTable dt = new DataTable();
        public MainWindow()
        {
            InitializeComponent();

            DataColumn dc = new DataColumn("count", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
           
            for (int i = 1; i < 11; i++)
            {
                dt.Rows.Add(new object[] { (i).ToString(), ("").ToString(), ("").ToString(), ("").ToString(), ("").ToString(), ("").ToString() });
            }
            dataGrid1.ItemsSource = dt.DefaultView; 
        }
    }
}
--------------------编程问答--------------------
引用 楼主 weiw_plaacc 的回复:
刚学C#,请问如何用代码实现datagridview添加固定行和列,如下图片,谢谢了。 
DataGridView
--------------------编程问答--------------------
引用 2 楼 maxldwy 的回复:
前台.xaml:
C# code?1234567891011121314151617181920<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http……



这个是刚刚参考的,怎么提示:错误 1 “System.Windows.Forms.DataGridView”不包含“ItemsSource”的定义,并且找不到可接受类型为“System.Windows.Forms.DataGridView”的第一个参数的扩展方法“ItemsSource”(是否缺少 using 指令或程序集引用?) E:\Vs-C#Study\mstest\mstest\Form1.cs 35 27 mstest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Data;
using System.Windows.Forms;

namespace mstest
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();

            DataColumn dc = new DataColumn("count", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);
            dc = new DataColumn("", typeof(System.String));
            dt.Columns.Add(dc);

            for (int i = 1; i < 11; i++)
            {
                dt.Rows.Add(new object[] { (i).ToString(), ("").ToString(), ("").ToString(), ("").ToString(), ("").ToString(), ("").ToString() });
            }
            dataGridView1.ItemsSource = dt.DefaultView; 
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
--------------------编程问答-------------------- 我也刚好用到 --------------------编程问答--------------------
引用 5 楼 u010148381 的回复:
我也刚好用到

你不要乱改命名空间 我新建的项目要是Wpf应用程序
项目名字就叫WpfApplication1  生的你不会乱改出错 --------------------编程问答--------------------
引用 4 楼 weiw_plaacc 的回复:
引用 2 楼 maxldwy 的回复:
前台.xaml:
C# code?1234567891011121314151617181920<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns……
你不要乱改命名空间 我新建的项目要是Wpf应用程序
项目名字就叫WpfApplication1  生的你不会乱改出错  --------------------编程问答--------------------
引用 4 楼 weiw_plaacc 的回复:
引用 2 楼 maxldwy 的回复:
前台.xaml:
C# code?1234567891011121314151617181920<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns……
而且如果你是直接把我给你的.xaml代码覆盖了你原来的文件的话,那么你的private void Form1_Load(object sender, EventArgs e)
和private void Form1_Load(object sender, EventArgs e)
事件就会报错 因为。xaml里没有你插入的Form
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,