使用默认值自动填充datagridview中的列 [英] Automatically fill column in datagridview with default value

查看:146
本文介绍了使用默认值自动填充datagridview中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我试图让我的datagridview自动用textbox zakce.Text中的值填充列akce你能提出我的想法吗?我试过这个:

Hello guys, Im trying to make my datagridview automatically fill column "akce" with value from textbox zakce.Text can you propose me any idea? I tried this:

public void dtg_sluzby_DefaultValuesNeeded(object sender,
System.Windows.Forms.DataGridViewRowEventArgs e)
{

    e.Row.Cells["akce"].Value = 999;

}
void vyber_sluzby()
{
    try
    {



        da1 = new SqlDataAdapter("select * from zajsluz WHERE akce=" + zakce.Text, spojeni);
        sqlCommandBuilder = new SqlCommandBuilder(da1);
        dt = new DataTable();
        da1.Fill(dt);
        dtg_sluzby.DataSource = dt;



但它不起作用:juset添加了一行包含其他值,但列akce为空。


but it didn't worked: It juset added a row with other values but column "akce" was empty.

推荐答案

如果 akce 字段存储文本,请尝试以这种方式更改查询:

If akce field stores text, try to change your query in this way:
"select * from zajsluz WHERE akce='" + zakce.Text + "'"



但请记住,在您后面的代码中构建查询会在 SQL注入攻击 [ ^ ];(



而不是在后面的代码中构建查询,使用存储过程 [ ^ ]。



演练:仅使用存储过程(C#) [ ^ ] - 关注相关链接

HOW TO:使用ADO.NET和Visual C#.NET调用参数化存储过程 [< a href =http://support.microsoft.com/kb/310070/en-ustarget =_ blanktitle =新窗口> ^ ]


But remember, building queries in code behind you are exposed on SQL Injection attack[^] ;(

Rather than building queries in code behind, use stored procedures[^].

Walkthrough: Using Only Stored Procedures (C#)[^] - follow related links
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]


这篇关于使用默认值自动填充datagridview中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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