Allright I have been toying with this and although for the clear help that Obislavu has given I don't seem to be able to get it to work.
This is how my Datagrid is initialised:
Code:
private void WinForm3_Load(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("user id=xxxxxxx;" + "password=xxxxx;server=x.x.x.x;" +
"Trusted_Connection=yes;" +
"database=xxxxx; " +
"connection timeout=30");
try {
SqlDataAdapter da = new SqlDataAdapter("SELECT name AS Router_Name FROM ROUTERS", myConnection);
// create a new dataset
DataSet ds = new DataSet();
// fill dataset
da.Fill(ds, "REMOVEROUTERS");
// Attach DataSet to DataGrid
//dataGrid1.DataSource = ds.DefaultViewManager;
dataGrid1.DataSource = ds.Tables["REMOVEROUTERS"].DefaultView;
// Close the Database connection
myConnection.Close();
}
catch(Exception griderror)
{
myConnection.Close();
MessageBox.Show(griderror.ToString(), "Error Message");
}
}
}
}
This is how my DataGrid is built up:
Code:
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.CaptionText = "Removable Routers";
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(32, 32);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(272, 240);
this.dataGrid1.TabIndex = 0;
This was all generated by Borland C# Builder and with some additional added programming ofcourse (many thanks to the people on this forum).
So what I want to do is add a delete button after each row that is displayed in the DataGrid. I have searched high and low for a way to do this in Borland itself but that appears to be impossible for some reason so I have to add the code myself. No problem if I can just find out what that code is ;-)
If I look at the code that Obislavu gave me I see that I have to build a class called DataGridButtonColumn with probably some methods in it. Currently I have no idea on how to proceed on this, I've searched on MSDN etc for a example of this but to no avail unfortunately.
Apparantly the line:
DataGridTableStyle vStyle = new DataGridTableStyle();
represents the table drawn by the System.Windows.Forms.DataGrid control at run time.
The line: vStyle.MappingName = ""; basically you give the name of the DataTable you use, as I use a DataSet I don't think this applies and it will remain blanc (or do I fill in the SQL Adapter name?)..
After that we get the reference to the class DataGridButtonColumn() with: DataGridBoolColumn vColumnStyleEdit = new DataGridButtonColumn();
This is the part I don't really understand, what will be in the class?
After this you get to define the column with the lines:
vColumnStyleEdit.AllowNull = false;
vColumnStyleEdit.HeaderText = "Edit";
vColumnStyleEdit.Alignment = System.Windows.Forms.HorizontalAlignment.Center ;
vColumnStyleEdit.Width = 60;
This speaks for itself, although no event handlers are present these must be defined here as well right?
Also where in the code must this all be placed, in the WinForm_Load correct?
Then you get the line:
vStyle.GridColumnStyles.Add( vColumnStyleEdit );
which I think will add the button to the GridColumnStyles but I do not see a reference to the DataGrid must this be made here?
You see a lot of questions and remarks, I hope someone can help me a bit further on this...(Obislavu?)
In any case thanks for the help already given and I hope to hear from someone soon...
Kind regards,
Raenius...
ps. Longest post ever! ;-)
"Free will...is an illusion"