1
Answer

How to Align columns header and content in WPF datagrid?

Abolfazl

Abolfazl

9y
696
1

Hi
i have create a wpf datagrid with auto generate column as below:

<DataGrid Name="dtg_student" Margin="10" FontStretch="Expanded" AutoGenerateColumns="True"></DataGrid>

i fill it by a table of database as below:

ProjectDatabaseDataContext db = new ProjectDatabaseDataContext();
dtg_student.ItemsSource = db.tbl_Students;

everything is ok but i want to set the columns header and columns content align to center. i can do this on Windows Form Application but i can't in WPF application.please help me to do it?

thanks 

 
 
Answers (1)
2
Ankit  Shukla

Ankit Shukla

NA 503 24.8k 9y
Hi, 
 
 Please write this code on source code:
 
 
<asp:Button ID="Delete" runat="server" onclick="Delete_Click"
OnClientClick="javascript: return confirm('Are you sure want to Delete?')"
Text="Delete" />
 
1
Govinda Rajulu Yemineni

Govinda Rajulu Yemineni

NA 1.5k 233.1k 9y
Hi,
 
generate OnRowDataBound event for Gridview and write the following code
 
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
}
}
}
}
 
for more information please refer the following URL
 
 http://www.aspsnippets.com/Articles/Delete-ASPNet-GridView-Row-with-JavaScript-Confirmation-Box-using-CommandField-and-OnRowDeleting-event.aspx
 
 If you find anything helpful please accept my answer
1
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 9y
1
Rajeesh Menoth

Rajeesh Menoth

NA 24.7k 628.9k 9y
1
Upendra Pratap Shahi

Upendra Pratap Shahi

NA 13.3k 861.3k 9y
Use below function-
 

function deleteArticle(hdnPostId, divContainer) {

if (confirm("Are you sure?"))

{

$.ajax({

type: "POST",

url: "TestPage.aspx/DeleteArticle",

data: "{PostId: '" + hdnPostId + "'}",

contentType: "application/json; charset=utf-8",

dataType: "json",

success: function (data) {

$('#' + divContainer).hide();

},

failure: function (errMsg) {

alert(errMsg);

}

});

}

return false;

}

 
in code behind call like this-
 

hypDelete.Attributes.Add("onclick","javascript:deleteArticle('"+hdnPostId.Value+"','"+divContainer.ClientID+"');return false;");

 
1
Govinda Rajulu Yemineni

Govinda Rajulu Yemineni

NA 1.5k 233.1k 9y
Hi,
 
define one button like this
 
<asp:LinkButton ID="lbDelete" runat="server" OnClick="lbDelete_Click" OnClientClick="return fnConfirm();"> Delete</asp:LinkButton> 
 
write javascript function for button like
 
<script type="text/javascript">
function fnConfirm() {
if (confirm("The item will be deleted. Are you sure want to continue?") == true)
return true;
else
return false;
}
 
in lbDelete_Click event write the logic for deletion of item 
 
for more information please refer the following link
 
http://www.dotnetpickles.com/2013/03/how-to-show-confirm-message-while.html 
 
If you find anything helpful please accept my answer 
 
1
Nilesh Jadav

Nilesh Jadav

7 23.8k 5.7m 9y
1
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 9y
Hi Murugavel,
 
Please find below code:  
 
<!DOCTYPE html>
<html>
   <body>
      <p>Click the button to display a confirm box.</p>
 
      <asp:Button ID="btnDelete" onclientclick="return myFunction()"          OnClick="CallServerMethod()"
      <p id="demo"></p>
 
      <script>
         function myFunction() {
            var result = confirm("Press a button!");
            if (result == true) {
               return true;
            } else {
               return false;
            }
         }
      </script>
   </body>
</html>
0
Murugavel S

Murugavel S

NA 81 7.7k 9y
Hello All,
 
         Thanks  a lot for ur Replies.. 
          Im sorry , in form I used <asp:commandfield> for delete .
         in that there is no property called  onclientclick and OnClick.
       so, how to display confirm msg before deleting item.?
      im using <asp:commandfield> for delete..
        Thanks in Advance