Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>



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 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();


       }


   }


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..
Posted
Comments
disha desani 9-Feb-12 5:46am    
These Question is very important for me...its required to bind the gridview with image...which are not stored in database(not path of imge also)...

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900