Thursday, February 9, 2012


       DataGridViewButtonColumn gvcol_UpdateRecord = new DataGridViewButtonColumn();
       DataGridViewButtonColumn gvcol_DeleteRecord = new DataGridViewButtonColumn();


 private void Configuration_Load(object sender, EventArgs e)
        {
         
            gv.EditMode = DataGridViewEditMode.EditProgrammatically;
            SetGridButtonColumn(gvcol_UpdateRecord, "Edit", 30, 0);
            SetGridButtonColumn(gvcol_DeleteRecord, "Delete", 42, 1);
            gv.Columns[0].Visible = false;
            gv.Columns[1].Visible = false;
        }


  private void  gv _CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           if (e.ColumnIndex == 0)
           {
               gv.EditMode = DataGridViewEditMode.EditOnEnter;
               if (e.RowIndex >= 0)
               {
                   gv.Rows[e.RowIndex].Cells[3].Selected = true;
                   gvcol_DeleteRecord.Text = "Update";
               }
           }
           else if (e.ColumnIndex == 1 && gv.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() == "Delete")
           {
               if (MessageBox.Show("Do you delete this records?", "Information..", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
               {
                   MessageBox.Show(gv.Rows[e.RowIndex].Cells[2].FormattedValue.ToString());
               }
           }
       }
       private void  gv _RowLeave(object sender, DataGridViewCellEventArgs e)
       {
           gvcol_DeleteRecord.Text = "Delete";
           gv.EditMode = DataGridViewEditMode.EditProgrammatically ;
       }
       private void  gv _CellValueChanged(object sender, DataGridViewCellEventArgs e)
       {
           gv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Honeydew;
       
       }
       private void  gv _MouseHover(object sender, EventArgs e)
       {
           gv.Columns[0].Visible = true ;
           gv.Columns[1].Visible = true;
       }
       private void  gv _MouseLeave(object sender, EventArgs e)
       {
           gv.Columns[0].Visible = false;
           gv.Columns[1].Visible = false;
       }
       private void SetGridButtonColumn(DataGridViewButtonColumn gvbtn, string text, int width, int index)
       {
           gvbtn.UseColumnTextForButtonValue = true;
           gvbtn.Text = text;
           gvbtn.Resizable = DataGridViewTriState.False;
           gvbtn.FlatStyle = FlatStyle.Popup;
           gvbtn.HeaderText = "";
           gvbtn.Width = width;
           gv.Columns.Insert(index, gvbtn);
       }
    }


Monday, March 22, 2010

Remove Duplicate values

CREATE TABLE dbo.authors
(
PK_Column INT IDENTITY,
Authorname VARCHAR(50)
)
GO
INSERT dbo.authors(Authorname)
SELECT 'Author1'
UNION ALL SELECT 'Author1'
UNION ALL SELECT 'Author1'
UNION ALL SELECT 'Author2'
UNION ALL SELECT 'Author2'
UNION ALL SELECT 'Author3'
UNION ALL SELECT 'Author3'
UNION ALL SELECT 'Author4'
GO
SELECT * FROM dbo.authors
Go
DELETE authors
FROM authors a
LEFT OUTER JOIN
(
SELECT Authorname, pk_column = MIN(PK_Column)
FROM dbo.authors
GROUP BY Authorname
) x
ON a.pk_column = x.pk_column
WHERE x.pk_column IS NULL

Monday, March 15, 2010

DoEvents in vb.net

If you call DoEvents in your code, your application can handle the other events.

Syntex:

Application.doevents()

Wednesday, October 21, 2009

Add image in Datagridview

Dim img As New DataGridViewImageColumn
With img
.DataPropertyName = "Picture"
.HeaderText = "Picture"
.Width = 20
' Specify path to a directory where image files are stored
.Image = ImageList1.Images(0)
End With
DataGridView1.Columns.Insert(0, img)

**************** update image dynamically ****************
If DataGridView1.Rows.Count = 0 Then
DataGridView1.Rows.Insert(0)
End If

With DataGridView1.Rows(0)
.Cells(0).Value = ImageList1.Images(iconImg)
.Cells(1).Value = strValue
End With
DataGridView1.Refresh()