C#gridview cellformatting错误“列不属于表” [英] C# gridview cellformatting error “column does not belong to table”

查看:128
本文介绍了C#gridview cellformatting错误“列不属于表”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GridViewImageColumn中设置图像。一切都好,但是当我添加新行时,我收到错误:列IsMetadata不属于表格。



我是什么尝试过:



我在按钮上添加新行:



I'm trying to set image in GridViewImageColumn. All is OK, but when I'm adding new row, I'm receiving error: column "IsMetadata" does not belong to table.

What I have tried:

I'm adding new row by buttom:

private void bt_AddNew_Click(object sender, EventArgs e)
{
     GridViewName.ReadOnly = false;
     GridViewName.AllowAddNewRow = false;
     GridViewName.Rows.AddNew();
}





然后我想在列column1中显示图像:





then I want to display in column "column1" image:

private void GridViewName_CellFormatting(object sender, CellFormattingEventArgs e)
{
      if (e.Column.Name == "column1")
      {
          if (e.CellElement.RowInfo.Cells["IsMetadata"].Value != DBNull.Value)
          {
              if (Convert.ToBoolean(e.CellElement.RowInfo.Cells["IsMetadata"].Value))
              { 
                  e.CellElement.Image = Accountant.Properties.Resources.WasashleliPapka;
                  e.CellElement.ImageAlignment = ContentAlignment.MiddleLeft;
                  e.CellElement.TextImageRelation = TextImageRelation.ImageBeforeText;
               }
           }
      }
 }





请帮助,如何避免这种情况错误:列不属于表



Please help, how to avoid this error: "column does not belong to table"

推荐答案

我们可以帮助您:我们在运行时无法访问您的GridView,或者您您的数据源 - 这就是您所需要的。



错误很明确:数据源中没有填充表的列,称为IsMetadata。

为什么不呢?我们不知道!



所以,它取决于你。

幸运的是,你有一个工具可供你使用将帮助您了解正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can;t help you with that: we have no access to your GridView while it is running, or you your data source at all - and that is what you need.

The error is clear: there is no column in the data source from which your table was populated that is called "IsMetadata".
Why not? We don't know!

So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于C#gridview cellformatting错误“列不属于表”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆