根据网格值在数据库视图中为每条记录显示Image(在ButtonField中) [英] Display the Image(in ButtonField) according to database value in gridview for each record

查看:55
本文介绍了根据网格值在数据库视图中为每条记录显示Image(在ButtonField中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的问题是最后一个..首先阅读下面对我的代码的解释...

我已经完成了这些工作(如下所述).
我通过单击更改网格视图中的图像,并根据该图像更改状态.
我的数据库就像..

ID ....................................数字
sub_tast_Name ...... nvarchar
状态......数字

Gridview cide如下所示

Hi,

My question is as last.. First read below explanation of my code...

I have done these(below explained) job..
I change the image in gridview by click on that and also change the status according to that.
My database is like..

ID........................Numeric
sub_tast_Name......nvarchar
Status..................numeric

Gridview cide is as below

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            GridLines="None" onrowdatabound="GridView1_RowDataBound" 
                onrowcommand="GridView1_RowCommand" 
             onselectedindexchanged="GridView1_SelectedIndexChanged" >
          
                <Columns>                      
                     <asp:ButtonField  CommandName="imgclick" ButtonType="Image" 
                         ImageUrl="~/Image/Black.png" AccessibleHeaderText="ImgButton"  
                         />
                     <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                         ReadOnly="True" SortExpression="ID"  
                         >



                     </asp:BoundField>
                     <asp:BoundField DataField="Sub_task_Name" HeaderText="Sub_task_Name" 
                         SortExpression="Sub_task_Name" />
                     <asp:BoundField DataField="Status" HeaderText="Status" 
                         SortExpression="Status" />
                </Columns>
            </asp:GridView>




第一次单击(在图像上),将图像从黑色更改为绿色,将状态从1更改为2.
第二次单击(在图像上),将图像从绿色更改为蓝色,状态从2更改为3.
第三次单击(在图像上),将图像从蓝色更改为红色,将状态从3更改为4.
第四次单击(在图像上),将图像从红色更改为黑色,将状态从4更改为1.


代码如下...




On 1st click(on Image) it change the image from BLACK to GREEN and STATUS from 1 to 2.
On 2nd click(on Image) it change the image from GREEN to BLUE and STATUS from 2 to 3.
On 3rd click(on Image) it change the image from BLUE to RED and STATUS from 3 to 4.
On 4th click(on Image) it change the image from RED to BLACK and STATUS from 4 to 1.


Code for that is below...

protected void Page_Load(object sender, EventArgs e)
   {
       con = new SqlConnection();
       var _with1 = con;
       _with1.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\MySampleDB.mdf;Integrated Security=True;User Instance=True";
       _with1.Open();

       Gv2Bind();

   }



Gridview绑定功能...



Gridview Bind Function...

private void Gv2Bind()
  {
      SqlCommand cmd = new SqlCommand("SELECT ID,Sub_task_Name,Status FROM SubTask", con);
      DataSet ds = new DataSet();
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      da.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
      //con.Close();
  }




更改状态和图像的代码是...




code of changing Status and Image is...

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
   {

   }
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {

   }

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {

       if (e.CommandName=="imgclick")
       {
           //int index = Convert.ToInt32(e.CommandArgument);
          // int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);

           int index = Convert.ToInt32(e.CommandArgument);

           int temp;
          temp = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);

         // gets the ID of clicked row from First Column(Name="Id")

           com = new SqlCommand();
           var _with1 = com;
           _with1.Connection = con;


           if (GridView1.Rows[index].Cells[3].Text == "1")
           {

               string str1;
               str1 = "update SubTask set Status=''2'' where ID= ''" + temp + "''";
               _with1.CommandText = str1;

               GridViewRow gvRow = GridView1.Rows[index];
               ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Green.png";


           }

           else if (GridView1.Rows[index].Cells[3].Text == "2")
           {

               string str1;
               str1 = "update SubTask set Status=''3'' where ID= ''" + temp + "''";
               _with1.CommandText = str1;

              GridViewRow gvRow = GridView1.Rows[index];
              ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Blue.png";

           }

           else if (GridView1.Rows[index].Cells[3].Text == "3")
           {

               string str1;
               str1 = "update SubTask set Status=''4'' where ID= ''" + temp + "''";
               _with1.CommandText = str1;

              GridViewRow gvRow = GridView1.Rows[index];
              ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Red.png";

           }

           else if (GridView1.Rows[index].Cells[3].Text == "4")
           {
               string str1;
               str1 = "update SubTask set Status=''1'' where ID= ''" + temp + "''";
               _with1.CommandText = str1;

               GridViewRow gvRow = GridView1.Rows[index];
              ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Black.png";


           }

           _with1.ExecuteNonQuery();


       }


   }



对于所有记录,第一次将Blavk图像设置为默认图像..


现在我的问题出现了,
假设有3条记录Sub_task_Name(即字段名称)
A
B
C
D
现在假设我将绿色图像设置为记录B
和红色图像记录D
然后我关闭该项目或webpgae..

现在,每当我运行该项目或Webform时.在记录B的ButtonField上必须显示绿色图像,而对于记录D的红色图像必须在页面加载时显示...

所以我该怎么做?

请帮忙..

在此先谢谢您..



For the First time for all record Blavk image is set as default..


Now my Question arises that
Suppose there are 3 records Sub_task_Name(i.e.Field name)
A
B
C
D
now suppose i set the GREEN image for record B
and RED image for record D
Then i close that project or webpgae..

Now whenever i run that project or webform . on the ButtonField of Record B must be display a GREEN image and for Record D RED image AT PAGE LOAD TIME...

so How can i do this??

Plese help..

Thanks in advance..

推荐答案

您需要存储设置这些颜色的状态,然后其余的都应该照顾好自己.您可以处理并发布给我们看的Rowdatabound,但没有写任何代码,它是设置这些颜色的地方,然后一切正常.
You need to store the state that sets these colors, then the rest should take care of itself. Rowdatabound, which you handle, and posted for us to see, but wrote no code for, is the place to be setting these colors, then it will all work.


这篇关于根据网格值在数据库视图中为每条记录显示Image(在ButtonField中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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